diff options
Diffstat (limited to 'cli/tests/integration/lsp_tests.rs')
-rw-r--r-- | cli/tests/integration/lsp_tests.rs | 382 |
1 files changed, 381 insertions, 1 deletions
diff --git a/cli/tests/integration/lsp_tests.rs b/cli/tests/integration/lsp_tests.rs index 198a3d022..d6a8b4757 100644 --- a/cli/tests/integration/lsp_tests.rs +++ b/cli/tests/integration/lsp_tests.rs @@ -4824,7 +4824,7 @@ fn lsp_completions_auto_import() { } #[test] -fn lsp_npm_completions_auto_import_and_quick_fix() { +fn lsp_npm_completions_auto_import_and_quick_fix_no_import_map() { let context = TestContextBuilder::new() .use_http_server() .use_temp_cwd() @@ -5074,6 +5074,386 @@ fn lsp_npm_completions_auto_import_and_quick_fix() { } #[test] +fn lsp_completions_auto_import_and_quick_fix_with_import_map() { + let context = TestContextBuilder::new() + .use_http_server() + .use_temp_cwd() + .build(); + let temp_dir = context.temp_dir(); + let import_map = r#"{ + "imports": { + "print_hello": "http://localhost:4545/subdir/print_hello.ts", + "chalk": "npm:chalk@~5", + "types-exports-subpaths/": "npm:/@denotest/types-exports-subpaths@1/" + } + }"#; + temp_dir.write("import_map.json", import_map); + + let mut client = context.new_lsp_command().build(); + client.initialize(|builder| { + builder.set_import_map("import_map.json"); + }); + client.did_open( + json!({ + "textDocument": { + "uri": "file:///a/file.ts", + "languageId": "typescript", + "version": 1, + "text": concat!( + "import {getClient} from 'npm:@denotest/types-exports-subpaths@1/client';\n", + "import _test1 from 'npm:chalk@^5.0';\n", + "import chalk from 'npm:chalk@~5';\n", + "import chalk from 'npm:chalk@~5';\n", + "import {printHello} from 'print_hello';\n", + "\n", + ), + } + }), + ); + client.write_request( + "deno/cache", + json!({ + "referrer": { + "uri": "file:///a/file.ts", + }, + "uris": [ + { + "uri": "npm:@denotest/types-exports-subpaths@1/client", + }, { + "uri": "npm:chalk@^5.0", + }, { + "uri": "npm:chalk@~5", + }, { + "uri": "http://localhost:4545/subdir/print_hello.ts", + } + ] + }), + ); + + // try auto-import with path + client.did_open(json!({ + "textDocument": { + "uri": "file:///a/a.ts", + "languageId": "typescript", + "version": 1, + "text": "getClie", + } + })); + let list = client.get_completion_list( + "file:///a/a.ts", + (0, 7), + json!({ "triggerKind": 1 }), + ); + assert!(!list.is_incomplete); + let item = list + .items + .iter() + .find(|item| item.label == "getClient") + .unwrap(); + + let res = client.write_request("completionItem/resolve", item); + assert_eq!( + res, + json!({ + "label": "getClient", + "kind": 3, + "detail": "function getClient(): 5", + "documentation": { + "kind": "markdown", + "value": "" + }, + "sortText": "16", + "additionalTextEdits": [ + { + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 0 } + }, + "newText": "import { getClient } from \"types-exports-subpaths/client\";\n\n" + } + ] + }) + ); + + // try quick fix with path + let diagnostics = client.did_open(json!({ + "textDocument": { + "uri": "file:///a/b.ts", + "languageId": "typescript", + "version": 1, + "text": "getClient", + } + })); + let diagnostics = diagnostics + .messages_with_file_and_source("file:///a/b.ts", "deno-ts") + .diagnostics; + let res = client.write_request( + "textDocument/codeAction", + json!(json!({ + "textDocument": { + "uri": "file:///a/b.ts" + }, + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 9 } + }, + "context": { + "diagnostics": diagnostics, + "only": ["quickfix"] + } + })), + ); + assert_eq!( + res, + json!([{ + "title": "Add import from \"types-exports-subpaths/client\"", + "kind": "quickfix", + "diagnostics": [ + { + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 9 } + }, + "severity": 1, + "code": 2304, + "source": "deno-ts", + "message": "Cannot find name 'getClient'.", + } + ], + "edit": { + "documentChanges": [{ + "textDocument": { + "uri": "file:///a/b.ts", + "version": 1, + }, + "edits": [{ + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 0 } + }, + "newText": "import { getClient } from \"types-exports-subpaths/client\";\n\n" + }] + }] + } + }]) + ); + + // try auto-import without path + client.did_open(json!({ + "textDocument": { + "uri": "file:///a/c.ts", + "languageId": "typescript", + "version": 1, + "text": "chal", + } + })); + + let list = client.get_completion_list( + "file:///a/c.ts", + (0, 4), + json!({ "triggerKind": 1 }), + ); + assert!(!list.is_incomplete); + let item = list + .items + .iter() + .find(|item| item.label == "chalk") + .unwrap(); + + let mut res = client.write_request("completionItem/resolve", item); + let obj = res.as_object_mut().unwrap(); + obj.remove("detail"); // not worth testing these + obj.remove("documentation"); + assert_eq!( + res, + json!({ + "label": "chalk", + "kind": 6, + "sortText": "16", + "additionalTextEdits": [ + { + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 0 } + }, + "newText": "import chalk from \"chalk\";\n\n" + } + ] + }) + ); + + // try quick fix without path + let diagnostics = client.did_open(json!({ + "textDocument": { + "uri": "file:///a/d.ts", + "languageId": "typescript", + "version": 1, + "text": "chalk", + } + })); + let diagnostics = diagnostics + .messages_with_file_and_source("file:///a/d.ts", "deno-ts") + .diagnostics; + let res = client.write_request( + "textDocument/codeAction", + json!(json!({ + "textDocument": { + "uri": "file:///a/d.ts" + }, + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 5 } + }, + "context": { + "diagnostics": diagnostics, + "only": ["quickfix"] + } + })), + ); + assert_eq!( + res, + json!([{ + "title": "Add import from \"chalk\"", + "kind": "quickfix", + "diagnostics": [ + { + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 5 } + }, + "severity": 1, + "code": 2304, + "source": "deno-ts", + "message": "Cannot find name 'chalk'.", + } + ], + "edit": { + "documentChanges": [{ + "textDocument": { + "uri": "file:///a/d.ts", + "version": 1, + }, + "edits": [{ + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 0 } + }, + "newText": "import chalk from \"chalk\";\n\n" + }] + }] + } + }]) + ); + + // try auto-import with http import map + client.did_open(json!({ + "textDocument": { + "uri": "file:///a/e.ts", + "languageId": "typescript", + "version": 1, + "text": "printH", + } + })); + + let list = client.get_completion_list( + "file:///a/e.ts", + (0, 6), + json!({ "triggerKind": 1 }), + ); + assert!(!list.is_incomplete); + let item = list + .items + .iter() + .find(|item| item.label == "printHello") + .unwrap(); + + let mut res = client.write_request("completionItem/resolve", item); + let obj = res.as_object_mut().unwrap(); + obj.remove("detail"); // not worth testing these + obj.remove("documentation"); + assert_eq!( + res, + json!({ + "label": "printHello", + "kind": 3, + "sortText": "16", + "additionalTextEdits": [ + { + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 0 } + }, + "newText": "import { printHello } from \"print_hello\";\n\n" + } + ] + }) + ); + + // try quick fix with http import + let diagnostics = client.did_open(json!({ + "textDocument": { + "uri": "file:///a/f.ts", + "languageId": "typescript", + "version": 1, + "text": "printHello", + } + })); + let diagnostics = diagnostics + .messages_with_file_and_source("file:///a/f.ts", "deno-ts") + .diagnostics; + let res = client.write_request( + "textDocument/codeAction", + json!(json!({ + "textDocument": { + "uri": "file:///a/f.ts" + }, + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 10 } + }, + "context": { + "diagnostics": diagnostics, + "only": ["quickfix"] + } + })), + ); + assert_eq!( + res, + json!([{ + "title": "Add import from \"print_hello\"", + "kind": "quickfix", + "diagnostics": [ + { + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 10 } + }, + "severity": 1, + "code": 2304, + "source": "deno-ts", + "message": "Cannot find name 'printHello'.", + } + ], + "edit": { + "documentChanges": [{ + "textDocument": { + "uri": "file:///a/f.ts", + "version": 1, + }, + "edits": [{ + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 0 } + }, + "newText": "import { printHello } from \"print_hello\";\n\n" + }] + }] + } + }]) + ); +} + +#[test] fn lsp_completions_snippet() { let context = TestContextBuilder::new().use_temp_cwd().build(); let mut client = context.new_lsp_command().build(); |