github-linguist / linguist

Language Savant. If your repository's language is being reported incorrectly, send us a pull request!
MIT License
12.22k stars 4.22k forks source link

Update the syntax of Autohotkey #6772

Open thqby opened 6 months ago

thqby commented 6 months ago

Describe the enhancement

AutoHotkey v2 has been officially released for more than a year, and it is the main version at present. In this month, v1 has stopped maintenance. AutoHotkey in github still uses the old syntax for language highlighting, so it's time to update it.

URL of syntax highlighting grammar

https://github.com/thqby/vscode-autohotkey2-lsp/blob/main/syntaxes/ahk2.tmLanguage.json

Most popular extensions

https://github.com/thqby/vscode-autohotkey2-lsp

Banaanae commented 4 months ago

I'm willing to work on this

Banaanae commented 4 months ago

I've look into this a bit more, and aren't too sure what to do.

While AutoHotkey has been released on V2, there are still many scripts using (the now deprecated) AutoHotkey V1. So I'm unsure whether we just update to V2 grammar (meaning deprecated V1 files may lose some highlighting), or if there is a solution to have multiple grammar files based on a RegEx (something like heuristics?)

If that second option isn't possible forcing V2 as the highlighted option would be the way forward?

lildude commented 4 months ago

We have a 1:1 relationship between languages and grammars.

The best solution is for the grammar to be backwardly compatible with the older version of the language.

The next best is to create a new entry for V2 as a new language, group the two, and use a heuristic (a regex) to differentiate the two. Keep in mind this V2 would be considered a completely new language and will need to meet our usage requirements before inclusion.

Switching out the current grammar for the newer and breaking syntax highlighting for older scripts is going to result in more work for the new grammar maintainers as any issues reported here will simply be redirected to the grammar maintainters to fix. This could in turn result in the "best solution" I previously mentioned 😁

thqby commented 4 months ago

https://github.com/AutoHotkey/AutoHotkeyUX/blob/main/launcher.ahk#L62-L81

The ahk launcher uses regular expressions to differentiate versions and start them. How to act as a heuristic (a regex)?

lildude commented 4 months ago

The CONTRIBUTING.md details how to add a new language and includes a link to the heuristics file where you can see how it's done with other languages.

thqby commented 4 months ago

I don't think heuristic is suitable for ahk. Only a small amount of code can be recognized by a single regular match.

The ahk launcher identifies the version by the difference in the number of features of the syntax of the two versions in the current code.

Banaanae commented 4 months ago

I think we can use multiple RegExs

The ahk launcher identifies the version by the difference in the number of features of the syntax of the two versions in the current code.

But sometimes this detection fails, is there anyway to set a default language?

thqby commented 4 months ago

Some features are misidentified if they occur in a multiline string.

multiline_string := "
(
  #Require AutoHotkey v1
)"

Or some of the features of v1 are valid in the expression of v2. ahk v1 command syntax

MsgBox, hello

ahk v2 expression syntax

hello := 'hello'
user_func(
  MsgBox, hello
)
DecimalTurn commented 3 months ago

But sometimes this detection fails, is there anyway to set a default language?

@Banaanae Yes, you could have something like this in the heuristics.yml file, so that if the regex doesn't match, for v2, it defaults to v1.

- extensions: ['.ahk']
  rules:
  - language: AutoHotkey v2
    pattern: '<your regex here>'
  - language: AutoHotkey v1

Also, you might want to use a named_pattern if you need multiple regexes as mentioned above. You'll find examples for this at the bottom of the heuristics.yml file. Note that those regexes can also be multiline mode to cover more complex syntax patterns than span multiple lines inside a script file.

DecimalTurn commented 3 months ago

Regarding whether or not V2 has reached a level of popularity that warrants a separate language, we're already at more than 2.9k files on GitHub using the .ahk extension that were written with AutoHotkey v2 : https://github.com/search?q=path%3A*.ahk+%22%23Requires+AutoHotkey+v2.0%22+NOT+is%3Afork&type=code