Skip to content

Add return type command for class names to language features #2266

Description

@ZachWatkins

It was suggested in pull request #2263 that I should see if the maintainers want a return type command I made for PHP to be available in the common language files (likely after modifications).

(Edit: Since this issue didn't receive a comment after the last maintainers meeting, I will assume it would be easier to discuss this if I submitted a PR. I will submit 2 separate PRs: (1) for the "returns class" command to be added to ./lang/tags/functions.talon, and (2) for the "returns global class" command to be added to ./lang/tags/functions.talon (since "returns global class" may not be necessary for other languages (see below)). I am hoping this makes it easier to discuss and review the different solutions that would add "returns class <user.text>" to the project. If one or both PRs are declined, then I will submit a third to add the declined change(s) to the PHP language file.)

Here is the code I submitted with recommended corrections:

returns class <user.text>
    formatted = user.formatted_text(text, "PUBLIC_CAMEL_CASE")
    insert(": {formatted}")

returns global class <user.text>:
    formatted = user.formatted_text(text, "PUBLIC_CAMEL_CASE")
    insert(": \\{formatted}")

The code above is fine for PHP, but aren't there languages which do not require camel case for their class names? If so, additional language-specific logic is required and possibly as a user setting as well. Edit: Since ./core/snippets/snippets/classDeclaration.snippet uses PUBLIC_CAMEL_CASE as the only insertion formatter, I will assume it is fine to use for these commands.

Edit:

About the "returns global class" command, other languages may not need this command. PHP uses namespaces for class, interface, function, and constant definitions. Namespaces use the \ character to declare subnamespaces, and a global namespace reference can be made by prefixing it with \. When a namespace is declared, namespace resolution first checks whether proceeding classes and functions are registered in that namespace. Prefixing them with "" explicitly declares they use the global space. Simple example:

<?php
namespace App;
function currentDate(): \DateTime {
    return new \DateTime();
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions