Skip to content

Commit

Permalink
Merge pull request #1470 from DanielXMoore/lsp-roots
Browse files Browse the repository at this point in the history
Improve project root detection in LSP
  • Loading branch information
edemaine authored Oct 16, 2024
2 parents 6cef3d8 + 0e1b32b commit ef029a4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions lsp/build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ node_modules/.bin/civet build/build.civet

mkdir -p dist/lib
cp node_modules/typescript/lib/lib.*.d.ts dist/lib
cp source/tsconfig.json dist/lib
31 changes: 27 additions & 4 deletions lsp/source/server.mts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,34 @@ const getProjectPathFromSourcePath = (sourcePath: string) => {
let projPath = sourcePathToProjectPathMap.get(sourcePath)
if (projPath) return projPath

const tsConfigPath = findConfigFile(sourcePath, tsSys.fileExists, 'tsconfig.json')
if (tsConfigPath) {
projPath = pathToFileURL(path.dirname(tsConfigPath) + "/").toString()
// If we're in a node_modules/foo directory, use that as the project path
let dirname = sourcePath
while (dirname.includes("node_modules")) {
if (path.basename(path.dirname(dirname)) === "node_modules") {
projPath = pathToFileURL(dirname + "/").toString()
break
} else {
dirname = path.dirname(dirname) // go up one level
}
}

// Otherwise, check for ancestor tsconfig
if (!projPath) {
const tsConfigPath = findConfigFile(sourcePath, tsSys.fileExists, 'tsconfig.json')
if (tsConfigPath) {
projPath = pathToFileURL(path.dirname(tsConfigPath) + "/").toString()
}
}
if (!projPath) projPath = rootUri // Fallback

// Otherwise, check whether we're inside the root
if (!projPath) {
if (sourcePath.startsWith(rootDir)) {
projPath = rootUri
} else {
projPath = pathToFileURL(path.dirname(sourcePath) + "/").toString()
}
}

sourcePathToProjectPathMap.set(sourcePath, projPath)
return projPath
}
Expand Down
7 changes: 7 additions & 0 deletions lsp/source/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This is the tsconfig used for interpreting lib files
{
"compilerOptions": {
"target": "esnext",
"lib": []
}
}

0 comments on commit ef029a4

Please sign in to comment.