Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vscode-json-languageserver support #4164

Merged
merged 12 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ale_linters/json/vscodejson.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
" Author: Dalius Dobravolskas <dalius.dobravolskas@gmail.com>
" Description: VSCode json languageserver

function! ale_linters#json#vscodejson#GetProjectRoot(buffer) abort
daliusd marked this conversation as resolved.
Show resolved Hide resolved
let l:git_path = ale#path#FindNearestDirectory(a:buffer, '.git')

return !empty(l:git_path) ? fnamemodify(l:git_path, ':h:h') : ''
endfunction

call ale#linter#Define('json', {
\ 'name': 'vscodejson',
\ 'lsp': 'stdio',
\ 'executable': 'vscode-json-languageserver',
\ 'command': '%e --stdio',
\ 'project_root': function('ale_linters#json#vscodejson#GetProjectRoot'),
\})
7 changes: 6 additions & 1 deletion autoload/ale/completion.vim
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,14 @@ function! ale#completion#ParseLSPCompletions(response) abort
continue
endif

if get(l:item, 'insertTextFormat', s:LSP_INSERT_TEXT_FORMAT_PLAIN) is s:LSP_INSERT_TEXT_FORMAT_PLAIN
let l:insertTextFormat = get(l:item, 'insertTextFormat', s:LSP_INSERT_TEXT_FORMAT_PLAIN)

if l:insertTextFormat is s:LSP_INSERT_TEXT_FORMAT_PLAIN
\&& type(get(l:item, 'textEdit')) is v:t_dict
let l:text = l:item.textEdit.newText
elseif l:insertTextFormat is s:LSP_INSERT_TEXT_FORMAT_SNIPPET
daliusd marked this conversation as resolved.
Show resolved Hide resolved
\&& !empty(l:item.label)
let l:text = l:item.label
elseif type(get(l:item, 'insertText')) is v:t_string
let l:text = l:item.insertText
else
Expand Down
2 changes: 1 addition & 1 deletion autoload/ale/linter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ let s:default_ale_linters = {
\ 'hack': ['hack'],
\ 'help': [],
\ 'inko': ['inko'],
\ 'json': ['jsonlint', 'spectral'],
\ 'json': ['jsonlint', 'spectral', 'vscodejson'],
\ 'json5': [],
\ 'jsonc': [],
\ 'perl': ['perlcritic'],
Expand Down
2 changes: 1 addition & 1 deletion autoload/ale/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ function! s:SendInitMessage(conn) abort
\ 'completion': {
\ 'dynamicRegistration': v:false,
\ 'completionItem': {
\ 'snippetSupport': v:false,
\ 'snippetSupport': v:true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunatelly I think this needs more evaluation. I did some tests and when this is enabled ALEImport breaks for me.
If enablinb snippetSupport is not critical for vscodejson I would recommend reverting this to get vscodejson merged and then handle adding snippetSupport on a separate issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely not critical. Let me fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hsanson BTW, how ALEImport breaks for you? What filetype you were workin on? I want to reproduce the problem and see if there is a fix.

Copy link
Contributor

@hsanson hsanson Apr 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple Java HelloWorld.java managed via Gradle and using eclipselsp:

public class HelloWorld {
    public static void main(String[] argv) {
        LocalDate today = LocalDate.now();
        System.out.println("Hello gradle world at " + today);
    }
}
  1. Put the cursos in LocalDate class and invoke :ALEImport.
  2. ALE should add the import statement "import java.time.LocalDate" at the beginning of the file.
  3. With the snippetSupport enabled this does not work.

\ 'commitCharactersSupport': v:false,
\ 'documentationFormat': ['plaintext'],
\ 'deprecatedSupport': v:false,
Expand Down
12 changes: 12 additions & 0 deletions doc/ale-json.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ g:ale_json_spectral_use_global *g:ale_json_spectral_use_global*

See |ale-integrations-local-executables|

===============================================================================
vscodejson *ale-json-vscode*

Website: https://www.npmjs.com/package/vscode-json-languageserver

Installation
-------------------------------------------------------------------------------

Install VSCode json language server either globally or locally: >

npm install -g vscode-json-languageserver
<

===============================================================================
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:
1 change: 1 addition & 0 deletions doc/ale-supported-languages-and-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ Notes:
* `tsserver`
* `xo`
* JSON
* `VSCode JSON language server`
* `cspell`
* `dprint`
* `eslint`
Expand Down
2 changes: 2 additions & 0 deletions doc/ale.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,7 @@ g:ale_linters *g:ale_linters*
\ 'help': [],
\ 'inko': ['inko'],
\ 'json': ['jsonlint', 'spectral'],
\ 'json': ['jsonlint', 'spectral', 'vscodejson'],
\ 'json5': [],
\ 'jsonc': [],
\ 'perl': ['perlcritic'],
Expand Down Expand Up @@ -2951,6 +2952,7 @@ documented in additional help files.
jq....................................|ale-json-jq|
prettier..............................|ale-json-prettier|
spectral..............................|ale-json-spectral|
vscodejson............................|ale-json-vscode|
daliusd marked this conversation as resolved.
Show resolved Hide resolved
jsonc...................................|ale-jsonc-options|
eslint................................|ale-jsonc-eslint|
jsonnet.................................|ale-jsonnet-options|
Expand Down
1 change: 1 addition & 0 deletions supported-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ formatting.
* [tsserver](https://github.com/Microsoft/TypeScript/wiki/Standalone-Server-%28tsserver%29)
* [xo](https://github.com/sindresorhus/xo)
* JSON
* [VSCode JSON language server](https://www.npmjs.com/package/vscode-json-languageserver)
* [cspell](https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell) :warning:
* [dprint](https://dprint.dev)
* [eslint](http://eslint.org/) :warning:
Expand Down
34 changes: 34 additions & 0 deletions test/completion/test_lsp_completion_parsing.vader
Original file line number Diff line number Diff line change
Expand Up @@ -734,3 +734,37 @@ Execute(Should still handle completion messages with empty additionalTextEdits w
\ ],
\ },
\ })

Execute(Should take label only from snippet completions):
let g:ale_completion_autoimport = 0

AssertEqual
\ [
\ {
\ 'word': 'lastName',
\ 'dup': 0,
\ 'menu': '',
\ 'info': 'The person''s last name.',
\ 'kind': 'm',
\ 'icase': 1,
\ 'user_data': json_encode({'_ale_completion_item': 1}),
\ }
\ ],
\ ale#completion#ParseLSPCompletions({
\ 'id': 5,
\ 'jsonrpc': '2.0',
\ 'result': {
\ 'isIncomplete': v:false,
\ 'items': [
\ {
\ 'label': 'lastName',
\ 'documentation': 'The person''s last name.',
\ 'kind': 10,
\ 'filterText': '"lastName"',
\ 'insertText': '"lastName": "$1",',
\ 'textEdit': {'range': {'end': {'character': 5, 'line': 3}, 'start': {'character': 2, 'line': 3}}, 'newText': '"lastName": "$1",'},
\ 'insertTextFormat': 2
\ }
\ ]
\ }
\ })
2 changes: 1 addition & 1 deletion test/lsp/test_lsp_startup.vader
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Before:
\ 'completion': {
\ 'dynamicRegistration': v:false,
\ 'completionItem': {
\ 'snippetSupport': v:false,
\ 'snippetSupport': v:true,
\ 'commitCharactersSupport': v:false,
\ 'documentationFormat': ['plaintext'],
\ 'deprecatedSupport': v:false,
Expand Down
2 changes: 1 addition & 1 deletion test/test_filetype_linter_defaults.vader
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Execute(The defaults for the inko filetype should be correct):
AssertEqual [], GetLinterNames('inko')

Execute(The defaults for the json filetype should be correct):
AssertEqual ['jsonlint', 'spectral'], GetLinterNames('json')
AssertEqual ['jsonlint', 'spectral', 'vscodejson'], GetLinterNames('json')

let g:ale_linters_explicit = 1

Expand Down