summaryrefslogtreecommitdiff
path: root/cli/tests/integration/lsp_tests.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tests/integration/lsp_tests.rs')
-rw-r--r--cli/tests/integration/lsp_tests.rs250
1 files changed, 250 insertions, 0 deletions
diff --git a/cli/tests/integration/lsp_tests.rs b/cli/tests/integration/lsp_tests.rs
index 52f1e55ba..f47fb7c82 100644
--- a/cli/tests/integration/lsp_tests.rs
+++ b/cli/tests/integration/lsp_tests.rs
@@ -4824,6 +4824,256 @@ fn lsp_completions_auto_import() {
}
#[test]
+fn lsp_npm_completions_auto_import_and_quick_fix() {
+ let context = TestContextBuilder::new()
+ .use_http_server()
+ .use_temp_cwd()
+ .build();
+ let mut client = context.new_lsp_command().build();
+ client.initialize_default();
+ client.did_open(
+ json!({
+ "textDocument": {
+ "uri": "file:///a/file.ts",
+ "languageId": "typescript",
+ "version": 1,
+ "text": "import {getClient} from 'npm:@denotest/types-exports-subpaths@1/client';import chalk from 'npm:chalk@5.0';\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",
+ }
+ ]
+ }),
+ );
+
+ // 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 \"npm:@denotest/types-exports-subpaths@1/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 \"npm:@denotest/types-exports-subpaths@1/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 \"npm:@denotest/types-exports-subpaths@1/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 \"npm:chalk@5.0\";\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 \"npm:chalk@5.0\"",
+ "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 \"npm:chalk@5.0\";\n\n"
+ }]
+ }]
+ }
+ }])
+ );
+}
+
+#[test]
fn lsp_completions_snippet() {
let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();