diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2022-02-10 07:13:50 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-10 07:13:50 +1100 |
commit | 773f882e5e7bcb93d4fd3ab66e56c6e422dfc97a (patch) | |
tree | a84c33fe3a666ab90f7b4c3e9c722e5533be1b90 /cli/tests/integration/lsp_tests.rs | |
parent | e218d567d5af0f778541c4b81f171d4fb1427db1 (diff) |
feat(lsp): provide completions from import map if available (#13624)
Closes #13619
Diffstat (limited to 'cli/tests/integration/lsp_tests.rs')
-rw-r--r-- | cli/tests/integration/lsp_tests.rs | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/cli/tests/integration/lsp_tests.rs b/cli/tests/integration/lsp_tests.rs index 7607582c8..d45b90955 100644 --- a/cli/tests/integration/lsp_tests.rs +++ b/cli/tests/integration/lsp_tests.rs @@ -548,6 +548,196 @@ fn lsp_import_assertions() { } #[test] +fn lsp_import_map_import_completions() { + let temp_dir = TempDir::new().expect("could not create temp dir"); + let mut params: lsp::InitializeParams = + serde_json::from_value(load_fixture("initialize_params.json")).unwrap(); + let import_map = + serde_json::to_vec_pretty(&load_fixture("import-map-completions.json")) + .unwrap(); + fs::write(temp_dir.path().join("import-map.json"), import_map).unwrap(); + fs::create_dir(temp_dir.path().join("lib")).unwrap(); + fs::write( + temp_dir.path().join("lib").join("b.ts"), + r#"export const b = "b";"#, + ) + .unwrap(); + + params.root_uri = Some(Url::from_file_path(temp_dir.path()).unwrap()); + if let Some(Value::Object(mut map)) = params.initialization_options { + map.insert("importMap".to_string(), json!("import-map.json")); + params.initialization_options = Some(Value::Object(map)); + } + + let deno_exe = deno_exe_path(); + let mut client = LspClient::new(&deno_exe).unwrap(); + client + .write_request::<_, _, Value>("initialize", params) + .unwrap(); + + client.write_notification("initialized", json!({})).unwrap(); + let uri = Url::from_file_path(temp_dir.path().join("a.ts")).unwrap(); + + did_open( + &mut client, + json!({ + "textDocument": { + "uri": uri, + "languageId": "typescript", + "version": 1, + "text": "import * as a from \"/~/b.ts\";\nimport * as b from \"\"" + } + }), + ); + + let (maybe_res, maybe_err) = client + .write_request( + "textDocument/completion", + json!({ + "textDocument": { + "uri": uri + }, + "position": { + "line": 1, + "character": 20 + }, + "context": { + "triggerKind": 2, + "triggerCharacter": "\"" + } + }), + ) + .unwrap(); + assert!(maybe_err.is_none()); + assert_eq!( + maybe_res, + Some(json!({ + "isIncomplete": false, + "items": [ + { + "label": ".", + "kind": 19, + "detail": "(local)", + "sortText": "1", + "insertText": "." + }, + { + "label": "..", + "kind": 19, + "detail": "(local)", + "sortText": "1", + "insertText": ".." + }, + { + "label": "std", + "kind": 19, + "detail": "(import map)", + "sortText": "std", + "insertText": "std" + }, + { + "label": "fs", + "kind": 17, + "detail": "(import map)", + "sortText": "fs", + "insertText": "fs" + }, + { + "label": "/~", + "kind": 19, + "detail": "(import map)", + "sortText": "/~", + "insertText": "/~" + } + ] + })) + ); + + client + .write_notification( + "textDocument/didChange", + json!({ + "textDocument": { + "uri": uri, + "version": 2 + }, + "contentChanges": [ + { + "range": { + "start": { + "line": 1, + "character": 20 + }, + "end": { + "line": 1, + "character": 20 + } + }, + "text": "/~/" + } + ] + }), + ) + .unwrap(); + let (method, _) = client.read_notification::<Value>().unwrap(); + assert_eq!(method, "textDocument/publishDiagnostics"); + let (method, _) = client.read_notification::<Value>().unwrap(); + assert_eq!(method, "textDocument/publishDiagnostics"); + let (method, _) = client.read_notification::<Value>().unwrap(); + assert_eq!(method, "textDocument/publishDiagnostics"); + + let (maybe_res, maybe_err) = client + .write_request( + "textDocument/completion", + json!({ + "textDocument": { + "uri": uri + }, + "position": { + "line": 1, + "character": 23 + }, + "context": { + "triggerKind": 2, + "triggerCharacter": "/" + } + }), + ) + .unwrap(); + assert!(maybe_err.is_none()); + assert_eq!( + maybe_res, + Some(json!({ + "isIncomplete": false, + "items": [ + { + "label": "b.ts", + "kind": 9, + "detail": "(import map)", + "sortText": "1", + "filterText": "/~/b.ts", + "textEdit": { + "range": { + "start": { + "line": 1, + "character": 20 + }, + "end": { + "line": 1, + "character": 23 + } + }, + "newText": "/~/b.ts" + } + } + ] + })) + ); + + shutdown(&mut client); +} + +#[test] fn lsp_hover() { let mut client = init("initialize_params.json"); did_open( |