From dedeb73ed0fad21be2eab8a6f0d9aa7e33075a4b Mon Sep 17 00:00:00 2001 From: S0AndS0 Date: Sun, 1 Sep 2024 14:24:31 -0700 Subject: [PATCH 1/5] Add configurations and notes for Astro LS --- README.md | 32 ++++++++++++++++++++++++++++ astro/install.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++ astro/package.json | 6 ++++++ astro/snippet.vim | 7 ++++++ 4 files changed, 98 insertions(+) create mode 100755 astro/install.py create mode 100644 astro/package.json create mode 100644 astro/snippet.vim diff --git a/README.md b/README.md index 9e6efc8..c2716eb 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ by YCM, though they should work for the most part. * [CSS](#css) * [PHP](#php) * [Crystal](#crystal) + * [Astro](#astro) * [Known Issues](#known-issues) @@ -347,6 +348,37 @@ let g:ycm_language_server = Place crystalline in the path (i.e. /usr/local/bin) or use absolute path in the example above.. +# Astro + +In addition to defining `g:ycm_language_server` block as shown in +[`astro/snippet.vim`](astro/snippet.vim) this LSP requires `.ycm_extra_conf.py` +to pass Language Server `initializationOptions` pointing to a directory +containing either `typescript.js` or `tsserverlibrary.js` file, such as +`node_modules/typescript/lib`, via `typescript.tsdk` key/value address, eg. + +```python +import os + +def Settings( **kwargs ): + if kwargs[ 'language' ] == 'astro': + configs = { + 'ls': { + 'typescript': { + 'tsdk': "{}/node_modules/typescript/lib".format(os.path.abspath(os.path.curdir)), + }, + }, + } + + return configs +``` + +... Authors of Astro Language Server recommend installing `prettier` plugins +too, so adding the following to your `npm init` rituals may be a good idea; + +```bash +npm install --save-dev typescript prettier prettier-plugin-astro +``` + # Jai This is using [Jails](https://github.comSogoCZE/Jails), which is very much diff --git a/astro/install.py b/astro/install.py new file mode 100755 index 0000000..f9cb3f5 --- /dev/null +++ b/astro/install.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +import subprocess, os, sys, platform + +def OnWindows(): + return platform.system() == 'Windows' + + +# On Windows, distutils.spawn.find_executable only works for .exe files +# but .bat and .cmd files are also executables, so we use our own +# implementation. +def FindExecutable( executable ): + # Executable extensions used on Windows + WIN_EXECUTABLE_EXTS = [ '.exe', '.bat', '.cmd' ] + + paths = os.environ[ 'PATH' ].split( os.pathsep ) + base, extension = os.path.splitext( executable ) + + if OnWindows() and extension.lower() not in WIN_EXECUTABLE_EXTS: + extensions = WIN_EXECUTABLE_EXTS + else: + extensions = [ '' ] + + for extension in extensions: + executable_name = executable + extension + if not os.path.isfile( executable_name ): + for path in paths: + executable_path = os.path.join( path, executable_name ) + if os.path.isfile( executable_path ): + return executable_path + else: + return executable_name + return None + + +def FindExecutableOrDie( executable, message ): + path = FindExecutable( executable ) + + if not path: + sys.exit( "ERROR: Unable to find executable '{0}'. {1}".format( + executable, + message ) ) + + return path + + +def Main(): + npm = FindExecutableOrDie( 'npm', 'npm is required to set up Astro LS.' ) + subprocess.check_call( [ npm, 'install', '--production' ] ) + + +if __name__ == '__main__': + Main() diff --git a/astro/package.json b/astro/package.json new file mode 100644 index 0000000..caafadb --- /dev/null +++ b/astro/package.json @@ -0,0 +1,6 @@ +{ + "description": "ycmd astro-ls runtime area", + "dependencies": { + "@astrojs/language-server": "2.14.1" + } +} diff --git a/astro/snippet.vim b/astro/snippet.vim new file mode 100644 index 0000000..56e9709 --- /dev/null +++ b/astro/snippet.vim @@ -0,0 +1,7 @@ +let g:ycm_language_server += [ + \ { + \ 'name': 'astro', + \ 'filetypes': [ 'astro' ], + \ 'cmdline': [ expand( g:ycm_lsp_dir . '/astro/node_modules/@astrojs/language-server/bin/nodeServer.js' ), '--stdio' ], + \ }, + \ ] From 89fb26367ee03e3c05bf380db42b75b707474ba2 Mon Sep 17 00:00:00 2001 From: S0AndS0 Date: Mon, 2 Sep 2024 10:20:41 -0700 Subject: [PATCH 2/5] Yoink configs from @astro/ts-plugin setup --- README.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c2716eb..91fe8ff 100644 --- a/README.md +++ b/README.md @@ -376,7 +376,22 @@ def Settings( **kwargs ): too, so adding the following to your `npm init` rituals may be a good idea; ```bash -npm install --save-dev typescript prettier prettier-plugin-astro +npm install --save-dev typescript prettier prettier-plugin-astro @astrojs/ts-plugin +``` + +Finally, hopefully for now, adding the `@astrojs/ts-plugin` to your project's +`tsconfig.json` may be necessary to enable all features of Astro LS + +```json +{ + "compilerOptions": { + "plugins": [ + { + "name": "@astrojs/ts-plugin" + } + ] + } +} ``` # Jai From 5b389e7aa7efea71bd7a21228a50acbd97fba60e Mon Sep 17 00:00:00 2001 From: S0AndS0 Date: Wed, 11 Sep 2024 13:53:16 -0700 Subject: [PATCH 3/5] Use locally scoped variable in Astro example --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 91fe8ff..abc5eb3 100644 --- a/README.md +++ b/README.md @@ -360,11 +360,12 @@ containing either `typescript.js` or `tsserverlibrary.js` file, such as import os def Settings( **kwargs ): + current_directory = os.path.abspath(os.path.curdir) if kwargs[ 'language' ] == 'astro': configs = { 'ls': { 'typescript': { - 'tsdk': "{}/node_modules/typescript/lib".format(os.path.abspath(os.path.curdir)), + 'tsdk': f"{current_directory}/node_modules/typescript/lib" }, }, } From 3cfdb9a6b6f973a5c1dce9f3bb3138283870c279 Mon Sep 17 00:00:00 2001 From: S0AndS0 Date: Wed, 11 Sep 2024 13:53:57 -0700 Subject: [PATCH 4/5] Allow any new versions of Astro LS to be installed --- astro/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astro/package.json b/astro/package.json index caafadb..1450ca4 100644 --- a/astro/package.json +++ b/astro/package.json @@ -1,6 +1,6 @@ { "description": "ycmd astro-ls runtime area", "dependencies": { - "@astrojs/language-server": "2.14.1" + "@astrojs/language-server": "*" } } From 13dbba611d05912dd80085ec80136c4d078c224d Mon Sep 17 00:00:00 2001 From: S0AndS0 Date: Wed, 11 Sep 2024 13:54:46 -0700 Subject: [PATCH 5/5] Add configurations for `project_root_files` --- astro/snippet.vim | 1 + 1 file changed, 1 insertion(+) diff --git a/astro/snippet.vim b/astro/snippet.vim index 56e9709..63f8f22 100644 --- a/astro/snippet.vim +++ b/astro/snippet.vim @@ -3,5 +3,6 @@ let g:ycm_language_server += [ \ 'name': 'astro', \ 'filetypes': [ 'astro' ], \ 'cmdline': [ expand( g:ycm_lsp_dir . '/astro/node_modules/@astrojs/language-server/bin/nodeServer.js' ), '--stdio' ], + \ 'project_root_files': [ 'tsconfig.json', 'astro.config.mjs' ] \ }, \ ]