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

fix(lsp): scope attribution for asset documents #24663

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion cli/lsp/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl AssetOrDocument {

pub fn scope(&self) -> Option<&ModuleSpecifier> {
match self {
AssetOrDocument::Asset(_) => None,
AssetOrDocument::Asset(asset_doc) => Some(asset_doc.specifier()),
AssetOrDocument::Document(doc) => doc.scope(),
}
}
Expand Down
14 changes: 14 additions & 0 deletions cli/tsc/99_main_compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ delete Object.prototype.__proto__;

const isCjsCache = new SpecifierIsCjsCache();

// Maps asset specifiers to the first scope that the asset was loaded into.
/** @type {Map<string, string | null>} */
const assetScopes = new Map();

/** @type {number | null} */
let projectVersionCache = null;

Expand Down Expand Up @@ -837,6 +841,9 @@ delete Object.prototype.__proto__;
}
const sourceFile = sourceFileCache.get(specifier);
if (sourceFile) {
if (!assetScopes.has(specifier)) {
assetScopes.set(specifier, lastRequestScope);
}
// This case only occurs for assets.
return ts.ScriptSnapshot.fromString(sourceFile.text);
}
Expand Down Expand Up @@ -1210,6 +1217,7 @@ delete Object.prototype.__proto__;
const newConfigsByScope = maybeChange[2];
if (newConfigsByScope) {
isNodeSourceFileCache.clear();
assetScopes.clear();
/** @type { typeof languageServiceEntries.byScope } */
const newByScope = new Map();
for (const [scope, config] of newConfigsByScope) {
Expand Down Expand Up @@ -1247,6 +1255,12 @@ delete Object.prototype.__proto__;
}
}

// For requests pertaining to an asset document, we make it so that the
// passed scope is just its own specifier. We map it to an actual scope here
// based on the first scope that the asset was loaded into.
if (scope?.startsWith(ASSETS_URL_PREFIX)) {
scope = assetScopes.get(scope) ?? null;
}
lastRequestMethod = method;
lastRequestScope = scope;
const ls = (scope ? languageServiceEntries.byScope.get(scope)?.ls : null) ??
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/lsp_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1409,11 +1409,13 @@ fn lsp_hover() {
#[test]
fn lsp_hover_asset() {
let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir();
temp_dir.write("deno.json", json!({}).to_string());
let mut client = context.new_lsp_command().build();
client.initialize_default();
client.did_open(json!({
"textDocument": {
"uri": "file:///a/file.ts",
"uri": temp_dir.uri().join("file.ts").unwrap(),
"languageId": "typescript",
"version": 1,
"text": "console.log(Date.now());\n"
Expand All @@ -1423,7 +1425,7 @@ fn lsp_hover_asset() {
"textDocument/definition",
json!({
"textDocument": {
"uri": "file:///a/file.ts"
"uri": temp_dir.uri().join("file.ts").unwrap()
},
"position": { "line": 0, "character": 14 }
}),
Expand Down