diff --git a/.vscode/settings.json b/.vscode/settings.json index 28eddc1..f91b6a6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "editor.insertSpaces": false, "tslint.enable": true, - "typescript.tsc.autoDetect": "off", - "typescript.preferences.quoteStyle": "single" + "js/ts.tsc.autoDetect": "off", + "js/ts.preferences.quoteStyle": "single" } \ No newline at end of file diff --git a/client/src/extension.ts b/client/src/extension.ts index 38e91b0..d1e8fa0 100644 --- a/client/src/extension.ts +++ b/client/src/extension.ts @@ -164,10 +164,13 @@ export async function activate(context: ExtensionContext) { // Resolve the ServerSpec for a document or workspace folder URI, prompting // for a missing password via the Server Manager's authentication provider. - async function resolveServerSpec(uri: Uri): Promise { + async function resolveServerSpec(uri: Uri): Promise { try { const wsFolderUriString = workspace.getWorkspaceFolder(uri)?.uri.toString(); - const serverSpec = objectScriptApi.serverForUri(uri)!; + const serverSpec = objectScriptApi.serverForUri(uri); + if (serverSpec === undefined) { + return; + } const auth = serverSpec.auth ?? new BasicAuthorization(serverSpec.username, serverSpec.password); if ( // Server was resolved @@ -231,18 +234,6 @@ export async function activate(context: ExtensionContext) { return server; } catch { // Treat any thrown error as "no server connection" - return { - serverName: "", - active: false, - apiVersion: 1, - serverVersion: "", - scheme: "http", - host: "", - port: 0, - pathPrefix: "", - namespace: "", - username: "", - }; } } diff --git a/client/tsconfig.json b/client/tsconfig.json index 479c618..db61596 100644 --- a/client/tsconfig.json +++ b/client/tsconfig.json @@ -7,7 +7,13 @@ "rootDir": "src", "sourceMap": true, "strictNullChecks": true, + "skipLibCheck": true, }, + "references": [ + { + "path": "../common" + } + ], "include": ["src"], "exclude": ["node_modules", ".vscode-test"] } diff --git a/common/src/types.ts b/common/src/types.ts index 34a0c7b..19a03b1 100644 --- a/common/src/types.ts +++ b/common/src/types.ts @@ -37,7 +37,7 @@ export interface GetTextParams { } export interface ProtocolMethods { - "intersystems/server/resolveFromUri": (_: string) => Promise; + "intersystems/server/resolveFromUri": (_: string) => Promise; "intersystems/uri/localToVirtual": (_: string) => string; "intersystems/uri/forDocument": (_: string) => string | null; "intersystems/uri/forTypeHierarchyClasses": (_: string[]) => string[]; diff --git a/common/tsconfig.json b/common/tsconfig.json index f973a88..5a05aeb 100644 --- a/common/tsconfig.json +++ b/common/tsconfig.json @@ -8,6 +8,7 @@ "sourceMap": true, "declaration": true, "declarationMap": true, + "composite": true, "strict": true, "esModuleInterop": true, "skipLibCheck": true, diff --git a/eslint.config.mjs b/eslint.config.mjs index bd0725c..4fc8daa 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -3,7 +3,14 @@ import tseslint from "typescript-eslint"; export default tseslint.config( { - ignores: ["**/vscode.d.ts", "**/vscode.proposed.d.ts", "client/out/**", "server/out/**", "**/*.config.{mjs,js}"], + ignores: [ + "**/vscode.d.ts", + "**/vscode.proposed.d.ts", + "client/out/**", + "server/out/**", + "common/out/**", + "**/*.config.{mjs,js}", + ], }, eslint.configs.recommended, ...tseslint.configs.recommended, diff --git a/package.json b/package.json index 241033d..718d2bf 100644 --- a/package.json +++ b/package.json @@ -1790,7 +1790,7 @@ "compile": "npm run compile:common && tsc -b", "compile:common": "cd common && npm run compile", "watch": "tsc -b -w", - "clean": "rimraf client/out && rimraf server/out && rimraf common/out", + "clean": "rimraf client/out && rimraf server/out && rimraf common/out && rimraf common/tsconfig.tsbuildinfo && rimraf client/tsconfig.tsbuildinfo && rimraf server/tsconfig.tsbuildinfo && rimraf tsconfig.tsbuildinfo", "lint": "eslint && prettier --check .", "lint-fix": "prettier --write . && eslint --fix", "postinstall": "cd client && npm install && cd ../server && npm install && cd ../common && npm install && cd .." diff --git a/server/src/providers/completion.ts b/server/src/providers/completion.ts index aa960cc..beae50a 100644 --- a/server/src/providers/completion.ts +++ b/server/src/providers/completion.ts @@ -567,7 +567,7 @@ class SchemaCache { async function completionFullClassName( doc: TextDocument, parsed: compressedline[], - server: ServerSpec, + server: ServerSpec | undefined, line: number, settings: LanguageServerConfiguration, ): Promise { @@ -633,7 +633,7 @@ async function completionFullClassName( * * @param server The server that this document is associated with. */ -async function completionPackage(server: ServerSpec, settings: LanguageServerConfiguration): Promise { +async function completionPackage(server: ServerSpec | undefined, settings: LanguageServerConfiguration): Promise { const result: CompletionItem[] = []; // Get all the packages @@ -661,7 +661,7 @@ async function completionPackage(server: ServerSpec, settings: LanguageServerCon * * @param server The server that this document is associated with. */ -async function completionInclude(server: ServerSpec): Promise { +async function completionInclude(server?: ServerSpec): Promise { const result: CompletionItem[] = []; // Get all inc files @@ -689,7 +689,7 @@ async function globalsOrRoutines( line: number, token: number, settings: LanguageServerConfiguration, - server: ServerSpec, + server: ServerSpec | undefined, lineText: string, prefix: string = "", ): Promise { @@ -790,7 +790,7 @@ export async function onCompletion(params: CompletionParams): Promise prevline.split(">").length) || - prevline.endsWith("<") || - prevline.endsWith('"') + prevline.endsWith("<") || + prevline.endsWith('"')) ) { // Get the SchemaCache for this server or create one if it doesn't exist let schemaCache = schemaCaches.get(server); @@ -2430,7 +2430,10 @@ export async function onCompletion(params: CompletionParams): Promise { if (Array.isArray(item.data) && item.data[0] === "class") { // Get the description for this class from the server - const server: ServerSpec = await getServerSpec(item.data[2]); + const server = await getServerSpec(item.data[2]); + if (!server) { + return item; + } const querydata: QueryData = { query: "SELECT Description FROM %Dictionary.ClassDefinition WHERE Name = ?", parameters: [item.data[1]], @@ -2445,7 +2448,10 @@ export async function onCompletionResolve(item: CompletionItem): Promise = new Map(); const classes: Map = new Map(); - // Keep track of current namespace for class/routine existence checks - const baseNs = server.namespace.toUpperCase(); - let currentNs = baseNs; - let nsNestedBlockLevel = 0; + // Keep track of current namespace for class/routine existence checks. + const nsContext = server?.namespace && { + server, + baseNs: server.namespace.toUpperCase(), + currentNs: server.namespace.toUpperCase(), + nsNestedBlockLevel: 0, + }; const validNsRegex = /^"[A-Za-z%]?[A-Za-z0-9-_]+"$/; // Store the ns, name and ranges of all classes and routines from other namespaces that we see @@ -611,11 +614,11 @@ export async function onDiagnostics(params: DocumentDiagnosticParams): Promise file.Name === classname + ".cls"); - if (filtered.length !== 1 && !classname.startsWith("%SYSTEM.")) { + if (nsContext && filtered.length !== 1 && !classname.startsWith("%SYSTEM.")) { diagnostics.push({ severity: DiagnosticSeverity.Warning, range: valrange, - message: `Class "${classname}" does not exist in namespace "${baseNs}"`, + message: `Class "${classname}" does not exist in namespace "${nsContext.baseNs}"`, source: "InterSystems Language Server", }); } @@ -694,6 +697,7 @@ export async function onDiagnostics(params: DocumentDiagnosticParams): Promise 0 && settings.diagnostics.classes && lastpkgend != pkgrange.end.character && @@ -703,7 +707,7 @@ export async function onDiagnostics(params: DocumentDiagnosticParams): Promise 0 && ((parsed[i][j].l == ld.cls_langindex && parsed[i][j].s == ld.cls_clsname_attrindex) || (parsed[i][j].l == ld.cos_langindex && parsed[i][j].s == ld.cos_clsname_attrindex)) && @@ -758,14 +763,14 @@ export async function onDiagnostics(params: DocumentDiagnosticParams): Promise 0 && ((parsed[i][j].l == ld.cls_langindex && parsed[i][j].s == ld.cls_rtnname_attrindex) || (parsed[i][j].l == ld.cos_langindex && parsed[i][j].s == ld.cos_rtnname_attrindex)) && @@ -851,14 +857,14 @@ export async function onDiagnostics(params: DocumentDiagnosticParams): Promise file.Name == word + ".inc")) { diagnostics.push({ severity: DiagnosticSeverity.Error, range: wordrange, - message: `Include file "${word}" does not exist in namespace "${baseNs}"`, + message: `Include file "${word}" does not exist in namespace "${nsContext.baseNs}"`, source: "InterSystems Language Server", }); } @@ -869,14 +875,14 @@ export async function onDiagnostics(params: DocumentDiagnosticParams): Promise 0 && @@ -907,7 +913,11 @@ export async function onDiagnostics(params: DocumentDiagnosticParams): Promise 0) { + if (nsContext && nsContext.nsNestedBlockLevel > 0) { // Determine if we're still in the different namespace if (parsed[i][j].l == ld.cos_langindex && parsed[i][j].s == ld.cos_brace_attrindex) { const brace = doc.getText(Range.create(i, parsed[i][j].p, i, parsed[i][j].p + parsed[i][j].c)); if (brace == "{") { - nsNestedBlockLevel++; + nsContext.nsNestedBlockLevel++; } else { - nsNestedBlockLevel--; - if (nsNestedBlockLevel == 0) { + nsContext.nsNestedBlockLevel--; + if (nsContext.nsNestedBlockLevel == 0) { // Ran off the end of that stack - currentNs = baseNs; + nsContext.currentNs = nsContext.baseNs; } } } else if ( @@ -1286,8 +1298,8 @@ export async function onDiagnostics(params: DocumentDiagnosticParams): Promise 0) { // We got data back @@ -1418,7 +1431,7 @@ export async function onDiagnostics(params: DocumentDiagnosticParams): Promise 0) { // We got data back diff --git a/server/src/providers/documentLink.ts b/server/src/providers/documentLink.ts index 94086ae..cc89055 100644 --- a/server/src/providers/documentLink.ts +++ b/server/src/providers/documentLink.ts @@ -2,7 +2,6 @@ import { DocumentLink, DocumentLinkParams, Range } from "vscode-languageserver/n import { documents } from "../utils/variables"; import * as ld from "../utils/languageDefinitions"; import { createDefinitionUri, getParsedDocument, getServerSpec, normalizeClassname } from "../utils/functions"; -import { ServerSpec } from "../utils/types"; export async function onDocumentLinks(params: DocumentLinkParams): Promise { const doc = documents.get(params.textDocument.uri); @@ -86,7 +85,10 @@ export async function onDocumentLinkResolve(link: DocumentLink): Promise { - const server: ServerSpec = await getServerSpec(params.uri); + const server = await getServerSpec(params.uri); + if (!server) { + return []; + } const result: QuickPickItem[] = []; const classname: string = params.classmame; @@ -730,7 +739,10 @@ export async function addMethod(params: AddMethodParams): Promise= 4) { comma = ", \n" + tab; } diff --git a/server/src/providers/signatureHelp.ts b/server/src/providers/signatureHelp.ts index a3bffb0..ed6156e 100644 --- a/server/src/providers/signatureHelp.ts +++ b/server/src/providers/signatureHelp.ts @@ -22,7 +22,7 @@ import { quoteUDLIdentifier, determineActiveParam, } from "../utils/functions"; -import { ServerSpec, SignatureHelpDocCache, SignatureHelpMacroContext } from "../utils/types"; +import { SignatureHelpDocCache, SignatureHelpMacroContext } from "../utils/types"; import { documents } from "../utils/variables"; import * as ld from "../utils/languageDefinitions"; @@ -170,7 +170,10 @@ export async function onSignatureHelp(params: SignatureHelpParams): Promise { const doc = documents.get(params.textDocument.uri); if (doc === undefined) { return null; @@ -24,7 +23,10 @@ export async function onTypeDefinition(params: TextDocumentPositionParams) { if (parsed === undefined) { return null; } - const server: ServerSpec = await getServerSpec(params.textDocument.uri); + const server = await getServerSpec(params.textDocument.uri); + if (!server) { + return + } if (parsed[params.position.line] === undefined) { // This is the blank last line of the file diff --git a/server/src/providers/typeHierarchy.ts b/server/src/providers/typeHierarchy.ts index 9ebeb32..8b565b3 100644 --- a/server/src/providers/typeHierarchy.ts +++ b/server/src/providers/typeHierarchy.ts @@ -32,7 +32,10 @@ export async function onPrepare(params: TypeHierarchyPrepareParams): Promise { let result: string[] = []; @@ -780,7 +780,7 @@ export async function normalizeClassname( doc: TextDocument, parsed: compressedline[], clsname: string, - server: ServerSpec, + server: ServerSpec | undefined, line: number, allfiles?: StudioOpenDialogFile[], possiblecls?: PossibleClasses, @@ -917,7 +917,7 @@ export async function getClassMemberContext( parsed: compressedline[], dot: number, line: number, - server: ServerSpec, + server?: ServerSpec, allfiles?: StudioOpenDialogFile[], inheritedpackages?: string[], ): Promise { @@ -1091,7 +1091,7 @@ export async function getClassMemberContext( ) == ","))) ) { // The token before the dot is a parameter, local variable, public variable or warning variable - const varClass = await determineVariableClass(doc, parsed, line, dot - 1, server, allfiles, inheritedpackages); + const varClass = server && await determineVariableClass(doc, parsed, line, dot - 1, server, allfiles, inheritedpackages); if (varClass) result = { baseclass: varClass, context: "instance" }; } else if ( dot > 0 && @@ -1210,13 +1210,13 @@ export async function makeRESTRequest( method: "GET" | "POST", api: number, path: string, - server: ServerSpec, + server?: ServerSpec, data?: any, checksum?: string, params?: any, ): Promise { // As of version 2.0.0, REST requests are made on the client side - return connection + return server && connection .sendRequest("intersystems/server/makeRESTRequest", { method, api, @@ -1234,14 +1234,16 @@ export async function makeRESTRequest( * * @param uri The TextDocument URI */ -export async function getServerSpec(uri: string): Promise { +export async function getServerSpec(uri: string): Promise { const spec = serverSpecs.get(uri); if (spec !== undefined) { return spec; } - const newspec: ServerSpec = await connection.sendRequest("intersystems/server/resolveFromUri", uri); - serverSpecs.set(uri, newspec); - return newspec; + const newspec = await connection.sendRequest("intersystems/server/resolveFromUri", uri); + if (newspec) { + serverSpecs.set(uri, newspec); + return newspec; + } } /** @@ -3252,13 +3254,13 @@ export async function getMemberType( tkn: number, cls: string, member: string, - server: ServerSpec, + server?: ServerSpec, ): Promise { if ( // We assume these methods always return an instance of the class (["%New", "%Open", "%OpenId"].includes(member) && parsed[line][tkn].s != ld.cos_attr_attrindex) || // Config class Open methods function like %Open(Id) - (cls.startsWith("Config.") && member == "Open" && server.namespace.toUpperCase() == "%SYS") + (cls.startsWith("Config.") && member == "Open" && server?.namespace.toUpperCase() == "%SYS") ) { return cls; } @@ -3496,7 +3498,10 @@ export function determineActiveParam(text: string): number { const showInternalCache: Map = new Map(); /** Determine if class members with the `Internal` keyword and system globals should be shown in the completion list */ -export async function showInternalForServer(server: ServerSpec): Promise { +export async function showInternalForServer(server?: ServerSpec): Promise { + if (! server) { + return false; + } const key = `${server.host}::${server.port}::${server.pathPrefix}::${server.username}`; let result = showInternalCache.get(key); if (result != undefined) return result; diff --git a/server/src/utils/types.ts b/server/src/utils/types.ts index ad81810..570207f 100644 --- a/server/src/utils/types.ts +++ b/server/src/utils/types.ts @@ -109,7 +109,7 @@ export type KeywordDoc = { constraint?: string | string[]; }; -export type { ServerSpec } from "../../../common/out/types"; +export type { ProtocolMethods, ServerSpec } from "../../../common/out/types"; /** * Context of the method/routine that a macro is in, including extra information needed for macro expansion. diff --git a/server/src/utils/variables.ts b/server/src/utils/variables.ts index 55cd5be..d0cd6e0 100644 --- a/server/src/utils/variables.ts +++ b/server/src/utils/variables.ts @@ -1,6 +1,6 @@ -import { createConnection, SemanticTokensBuilder, TextDocuments } from "vscode-languageserver/node"; +import { Connection, createConnection, SemanticTokensBuilder, TextDocuments } from "vscode-languageserver/node"; import { TextDocument } from "vscode-languageserver-textdocument"; -import { compressedline, LanguageServerConfiguration, ServerSpec } from "./types"; +import { compressedline, LanguageServerConfiguration, ProtocolMethods, ServerSpec } from "./types"; /** * TextDocument URI's mapped to the tokenized representation of the document. @@ -10,7 +10,12 @@ export const parsedDocuments: Map = new Ma /** * Node IPC connection between the server and client. */ -export const connection = createConnection(); +export const connection: Omit & { + sendRequest( + method: K, + params: Parameters[0], + ): Promise>>; +} = createConnection(); /** * TextDocument manager. diff --git a/server/tsconfig.json b/server/tsconfig.json index 9326bc7..2c2e5d2 100644 --- a/server/tsconfig.json +++ b/server/tsconfig.json @@ -11,8 +11,14 @@ "noImplicitAny": false, "resolveJsonModule": true, "esModuleInterop": true, - "allowJs": true + "allowJs": true, + "skipLibCheck": true }, + "references": [ + { + "path": "../common" + } + ], "include": ["src"], "exclude": ["node_modules", ".vscode-test"] } diff --git a/tsconfig.json b/tsconfig.json index e5aa540..193ee96 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,9 @@ { "files": [], "references": [ + { + "path": "./common" + }, { "path": "./client" },