AutoHotkey v2 Language Support, used in any editor that supports lsp

Scripting and setups with Visual Studio Code (vscode) and AutoHotkey.
User avatar
thqby
Posts: 391
Joined: 16 Apr 2021, 11:18
Contact:

AutoHotkey v2 Language Support, used in any editor that supports lsp

Post by thqby » 20 Jun 2021, 04:10

I have developed a vscode plugin, most of the functionality is implemented using the vscode language service protocol. The plugin supports renaming symbols, error diagnosis, IntelliSense, params hints, outlining, code tags, color hints, hover hints, go to definition, find all references, code formatting, etc.
It can also be used with other editors that support lsp, such as neovim.

more detail:
https://github.com/thqby/vscode-autohotkey2-lsp
https://marketplace.visualstudio.com/items?itemName=thqby.vscode-autohotkey2-lsp


lsp-server download(using in non-vscode)

Code: Select all

# curl & node
mkdir vscode-autohotkey2-lsp
cd vscode-autohotkey2-lsp
curl -L -o install.js https://raw.githubusercontent.com/thqby/vscode-autohotkey2-lsp/main/tools/install.js
node install.js
Last edited by thqby on 12 Feb 2023, 04:35, edited 5 times in total.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: vscode language server for ahk v2

Post by swagfag » 20 Jun 2021, 06:51

niiice
what v2 version is this targeting? for example ByRef isnt being detected as an error, yet the hover tooltip seems to suggest the correct usage
image.png
image.png (6.11 KiB) Viewed 11935 times
the callsite doesnt say anything either, but i guess it cant figure out if arg1 containing a VarRef is being passed in, so whatever

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: vscode language server for ahk v2

Post by boiler » 20 Jun 2021, 07:39

This looks really nice. Do you happen to have plans to make a v1 version? Until v2 becomes the official release (or at least beta, perhaps), I’ll continue to use VSCode extensions that support v1 or both.

User avatar
thqby
Posts: 391
Joined: 16 Apr 2021, 11:18
Contact:

Re: vscode language server for ahk v2

Post by thqby » 20 Jun 2021, 08:03

swagfag wrote:
20 Jun 2021, 06:51
niiice
what v2 version is this targeting? for example ByRef isnt being detected as an error, yet the hover tooltip seems to suggest the correct usage
image.png
the callsite doesnt say anything either, but i guess it cant figure out if arg1 containing a VarRef is being passed in, so whatever
For the current version, some syntax from previous versions has not been completely removed.

User avatar
thqby
Posts: 391
Joined: 16 Apr 2021, 11:18
Contact:

Re: vscode language server for ahk v2

Post by thqby » 20 Jun 2021, 08:09

boiler wrote:
20 Jun 2021, 07:39
This looks really nice. Do you happen to have plans to make a v1 version? Until v2 becomes the official release (or at least beta, perhaps), I’ll continue to use VSCode extensions that support v1 or both.
There are already many people developing v1 versions of the plugin, and I rarely use v1 versions anymore.
Different ahk plugins can be enabled for different workspaces or folders.

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: vscode language server for ahk v2

Post by boiler » 20 Jun 2021, 12:17

Thanks for that info.

User avatar
thqby
Posts: 391
Joined: 16 Apr 2021, 11:18
Contact:

Re: vscode language server for ahk v2

Post by thqby » 26 Aug 2021, 22:12

Running LSP plugin in vim 8.2.
Screenshot_20210827_102022_com.termux.jpg
Screenshot_20210827_102022_com.termux.jpg (98.33 KiB) Viewed 11176 times

User avatar
DuyMinh
Posts: 40
Joined: 05 May 2017, 08:34

Re: vscode language server for ahk v2

Post by DuyMinh » 17 Sep 2021, 04:19

Well, I'm currently using this extension which is very good to me, but I want to ask how to add my customized function/class to display for VSCode, in SciTE4AutoHotkey, this can be easy by adding it to ahk.api and ahk.keywords.properties so as to highlight it... I try to search on google but couldn't find anything helpful... Would you help me with this please? Many thanks.

User avatar
thqby
Posts: 391
Joined: 16 Apr 2021, 11:18
Contact:

Re: vscode language server for ahk v2

Post by thqby » 17 Sep 2021, 04:47

Clicking (Ctrl + LButton) on the built-in function (such as msgbox) will jump to the AHK header file(ahk2.h.ahk or ahk2_h.h.ahk), edit and save the file.

Remember to backup. Updating the plug-in will lose the modified file.

In addition, #include libfiles will also prompt for customized functions or classes.

User avatar
DuyMinh
Posts: 40
Joined: 05 May 2017, 08:34

Re: vscode language server for ahk v2

Post by DuyMinh » 17 Sep 2021, 06:03

thqby wrote:
17 Sep 2021, 04:47
Clicking (Ctrl + LButton) on the built-in function (such as msgbox) will jump to the AHK header file(ahk2.h.ahk or ahk2_h.h.ahk), edit and save the file.

Remember to backup. Updating the plug-in will lose the modified file.

In addition, #include libfiles will also prompt for customized functions or classes.
Many thanks again, this is really helpful for me... :bravo:

DaveT1
Posts: 218
Joined: 07 Oct 2014, 11:23

Re: vscode language server for ahk v2

Post by DaveT1 » 24 Nov 2021, 11:54

Hi, just came across this extension which seems very good :bravo:

Sadly I'm a newbie and can't make script debugging work. My extension settings for your extension look like:
image.png
image.png (44.87 KiB) Viewed 9870 times
and I've got the vscode-autohotkey-debug enabed.

When I run with or without debugging I get this error:
image.png
image.png (5.91 KiB) Viewed 9870 times
I'm not clear if this is an error from vscode, vscode-autohotkey-debug, or from AutoHotKey2 Language Support?

My launch.json file looks like:
image.png
image.png (19.07 KiB) Viewed 9870 times
Do I need to add something here?

Many thanks.

User avatar
thqby
Posts: 391
Joined: 16 Apr 2021, 11:18
Contact:

Re: vscode language server for ahk v2

Post by thqby » 26 Nov 2021, 07:53

F5 or context menu - debug.
launch.json is not necessary.

DaveT1
Posts: 218
Joined: 07 Oct 2014, 11:23

Re: vscode language server for ahk v2

Post by DaveT1 » 26 Nov 2021, 11:49

thqby wrote:
26 Nov 2021, 07:53
F5 or context menu - debug.
launch.json is not necessary.
Thanks @thqby. I seem to have better progress now, but can only run and debug in AHKv1. I'll try to ask zero-pluplus how to set up his extension.

Thanks again for the reply.

User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: vscode language server for ahk v2

Post by JoeSchmoe » 21 Jan 2022, 18:36

thqby wrote:
26 Aug 2021, 22:12
Running LSP plugin in vim 8.2.
Thank you for this! :bravo: This looks terrific. I am eager to use this with neovim.

These days, Neovimmers are moving toward lsp-config rather than coc-nvim. Would you be willing to push a config-file for your lsp to lsp-config? It would look like this (this is the config-file for the phpactor LSP):

Code: Select all

local util = require 'lspconfig.util'

return {
  default_config = {
    cmd = { 'phpactor', 'language-server' },
    filetypes = { 'php' },
    root_dir = function(pattern)
      local cwd = vim.loop.cwd()
      local root = util.root_pattern('composer.json', '.git')(pattern)

      -- prefer cwd if root is a descendant
      return util.path.is_descendant(cwd, root) and cwd or root
    end,
  },
  docs = {
    description = [[
https://github.com/phpactor/phpactor
Installation: https://phpactor.readthedocs.io/en/master/usage/standalone.html#global-installation
]],
    default_config = {
      cmd = { 'phpactor', 'language-server' },
      root_dir = [[root_pattern("composer.json", ".git")]],
    },
  },
}
Simple instructions are here.

I am just a 40-something finance and economics teacher and only code a couple times a year, so all of this is quite above my head. However, I use LunarVim, a great IDE built off Neovim. If you could get the server in lsp-config, I may be able to push commits to get it in LunarVim.

Pinging @guest3456, as I know you are a vimmer, too. Thank you for your work on that and for the AHK1->AHK2 conversion script.

Finally, one last smooth-brain question for @thqby that may show you what a noob I am. I didn't see an executable in your code on github. To install your LSP in Neovim do we have to install the LSP via the VisualStudio Marketplace first?

User avatar
thqby
Posts: 391
Joined: 16 Apr 2021, 11:18
Contact:

Re: vscode language server for ahk v2

Post by thqby » 22 Jan 2022, 01:27

@JoeSchmoe
Plug-in is implemented in the JavaScript language. Nodejs is required for non-VS Code and Web environments.
This is vscode web.

1.install nodejs
2.download lsp plug-in from VisualStudio Marketplace
3.use 7z or other decompression tools, extract the following files server/*, syntaxes/*, package.nls.json from lsp plug-in.vsix
4.configure LSP settings for the editor

Code: Select all

default_config = {
    cmd = { 'node.exe', 'ahk2-lsp folder/server/dist/server.js', '--stdio' },
    cmd_env = {
        AHK2_LS_CONFIG = "{\"locale\":\"en-us\",\"InterpreterPath\":\"C:\\\\Program Files\\\\Autohotkey\\\\AutoHotkey.exe\",\"AutoLibInclude\":0}"  
    },
    filetypes = { 'ah2', 'ahk' }
}

User avatar
thqby
Posts: 391
Joined: 16 Apr 2021, 11:18
Contact:

Re: vscode language server for ahk v2

Post by thqby » 22 Jan 2022, 06:30

Sublime Text4 lsp setting

Code: Select all

{
    "clients": {
        "ahk2-lsp": {
            "enabled": true,
            "command": ["node.exe", "thqby.vscode-autohotkey2-lsp/server/dist/server.js", "--stdio"], // Update the PATH
            "selector": "source.ahk2",
            "env": {
                "AHK2_LS_CONFIG": "{\"locale\":\"en-us\",\"InterpreterPath\":\"C:/Program Files/Autohotkey/AutoHotkey32.exe\",\"AutoLibInclude\":0}"
            },
            "schemes": ["file", "buffer", "res"]
        }
    }
}
coc.nvim setting

Code: Select all

{
	"languageserver": {
		"autohotkey": {
			"module": "thqby.vscode-autohotkey2-lsp/server/dist/server.js",
			"filetypes": [ "ahk", "ah2", "autohotkey", "autohotkey2" ],
			"args": ["--node-ipc"],
			"env": {
				"AHK2_LS_CONFIG": "{\"locale\":\"en-us\",\"InterpreterPath\":\"C:/Program Files/Autohotkey/AutoHotkey32.exe\",\"AutoLibInclude\":0}"
			}
		}
	},
	"suggest.snippetsSupport": true
}

User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: vscode language server for ahk v2

Post by JoeSchmoe » 22 Jan 2022, 12:38

Many thanks! It's still a bit overwhelming, but I'll see if I can get it working.

If anyone wants to help or follow along, I'm hanging out with the same username in the LunarVim discord ( https://discord.gg/Xb9B4Ny ), trying to get help in the #neovim and #windows channels. It's a great community and reminds me of AHK in the old days (early aughts). They are Linux and LunarVim oriented. I follow along because I use LunarVim in the Windows Subsystem for Linux (WSL2) on my Windows 11 machine. (frankly, it's made me want to install Linux on my old laptop, though) We've been discussing AHK a bit in the #windows channel. There is much respect for AHK and what it can do (they are consummate hackers).

User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: vscode language server for ahk v2

Post by JoeSchmoe » 23 Jan 2022, 00:28

Welp, I put in my four hours and I'm afraid it's above my head. I do hope you'll do a PR on nvim-lspconfig, as it appears it's becoming a new standard. If you do, I'll be happy to get it in LunarVim.

User avatar
thqby
Posts: 391
Joined: 16 Apr 2021, 11:18
Contact:

Re: vscode language server for ahk v2

Post by thqby » 23 Jan 2022, 01:52

I looked at it and found that the configuration is much more complex than that of coc.nvim

I am not familiar with lua and vim

User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: vscode language server for ahk v2

Post by JoeSchmoe » 23 Jan 2022, 11:35

thqby wrote:
23 Jan 2022, 01:52
I looked at it and found that the configuration is much more complex than that of coc.nvim

I am not familiar with lua and vim
Hi, thqby, thanks so much for following up!

Yeah, the configuration with lspconfig freaked me out at first, but I really want this, so I looked into it and it's actually quite simple. Basically, coc.nvim is pretty bloated, so in version 0.5, the neovim devs integrated LSP into the neovim natively. Because each LSP Server is different, they made a plugin, lspconfig, to configure LSP. For each language server, lspconfig just needs a short configuration file and it loads and configures Neovim's native LSP client.

Having Kylo's help on this is a real gamechanger. He knows neovim, lspconfig, and lua inside and out (at least compared to us - he's a dev on a project that integrates all three technologies). He's provided the configuration file that lspconfig needs to load your LSP server in the issue he file, so basically if we're ever going to solve this, now might be the time.

EDIT: I think I understand what you mean by "getDllExport currently implemented on client." Are you saying that you'll need to update the code of the language server so that functionality managing getDllExport is handled exclusively on the server without any participation of the client? Presumably, this will be required to get the server to work with neovim's native LSP client?

BTW, as someone who just learned Lua, I can say it's a piece of cake and a pleasure to learn. Back in the day, before Lexikos took over, there was even some talk of migrating AHK to Lua (essentially as a Lua Library) instead of continuing to expand the AHK language. The only thing one needs to learn for this is their basic associative arrays, called tables, and their "require" command. Basically, the first half of this page: https://learnxinyminutes.com/docs/lua/ (no need to learn metatables, which are how Lua does objects - very similar to how AHK does).
Last edited by JoeSchmoe on 23 Jan 2022, 19:27, edited 1 time in total.

Post Reply

Return to “Visual Studio Code”