From a49108bea37b1b8f607af4e61c8b91615fb7c464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=A8=E5=8D=93?= Date: Thu, 10 Nov 2022 20:42:06 +0800 Subject: [PATCH] Go to defination: go to included module/file --- server/src/server.ts | 4 ++-- server/src/symbolTable/goToDefination.ts | 18 ++++++++++++++++++ server/src/utils.ts | 17 ++++++++++++++--- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/server/src/server.ts b/server/src/server.ts index 44252c3..7ffbf33 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -294,8 +294,8 @@ function getWordAtPosition(textDocument: TextDocument, position: Position): Word start = line.substring(0, position.character), end = line.substring(position.character); // TODO: the regex expression capture numbers, fix it. - const startReg = /[a-zA-Z0-9_]*$/, - endReg = /^[a-zA-Z0-9_]*/; + const startReg = /[a-zA-Z0-9_\.\/]*$/, + endReg = /^[a-zA-Z0-9_\.\/]*/; const startWord = start.match(startReg)[0], endWord = end.match(endReg)[0]; diff --git a/server/src/symbolTable/goToDefination.ts b/server/src/symbolTable/goToDefination.ts index f14a369..a4b2d70 100644 --- a/server/src/symbolTable/goToDefination.ts +++ b/server/src/symbolTable/goToDefination.ts @@ -80,6 +80,24 @@ export class DefinationListener extends CMakeListener { if (!fileUri) { return; } + + // add included module to refDef + const refPos: string = this.uri + '_' + (nameToken.line - 1) + '_' + + nameToken.column + '_' + nameToken.text; + refToDef.set(refPos, { + uri: fileUri, + range: { + start: { + line: 0, + character: 0 + }, + end: { + line: Number.MAX_VALUE, + character: Number.MAX_VALUE + } + } + }); + const tree = getFileContext(fileUri); const definationListener = new DefinationListener(fileUri, this.currentScope); antlr4.tree.ParseTreeWalker.DEFAULT.walk(definationListener, tree); diff --git a/server/src/utils.ts b/server/src/utils.ts index f5dd27b..a0d9f8f 100644 --- a/server/src/utils.ts +++ b/server/src/utils.ts @@ -68,6 +68,10 @@ export function getIncludeFileUri(currentFileUri: string, includeFileName: strin return includeFileUri; } + // TODO: if includeFileName contains variable reference + // ex: include(${CMAKE_CURRENT_LIST_DIR}/xxx.cmake) + // Module: AndroidTestUtilities + // name is a cmake module const cmakePath: string = which('cmake'); if (cmakePath === null) { @@ -75,20 +79,27 @@ export function getIncludeFileUri(currentFileUri: string, includeFileName: strin } const moduleDir = 'cmake-' + cmakeVersion.major + '.' + cmakeVersion.minor; - const resPath = path.join(cmakePath, '..', 'share', moduleDir, 'Modules', includeFileName) + '.cmake'; + const resPath = path.join(cmakePath, '../..', 'share', moduleDir, 'Modules', includeFileName) + '.cmake'; - return pathToFileURL(resPath).toString(); + if (existsSync(resPath)) { + return pathToFileURL(resPath).toString(); + } + + return null; } function which(cmd: string): string { let command: string; + let pathEnvSep: string; if (os.type() === 'Windows_NT') { command = cmd + ".exe"; + pathEnvSep = ';'; } else { command = cmd; + pathEnvSep = ':'; } - for (const dir of process.env.PATH.split(path.sep)) { + for (const dir of process.env.PATH.split(pathEnvSep)) { const absPath: string = dir + path.sep + command; if (existsSync(absPath)) { return absPath;