Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Use definition to indicate out-of-project files with server support #253

Closed
wants to merge 2 commits into from
Closed
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
30 changes: 29 additions & 1 deletion lib/auto-languageclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ export default class AutoLanguageClient {
connection,
capabilities: initializeResponse.capabilities,
disposable: new CompositeDisposable(),
additionalPaths: new Set(),
considerDefinitionPath: (defPath: string): void => {
if (!defPath.startsWith(projectPath)) {
newServer.additionalPaths.add(path.dirname(defPath));
}
},
};
this.postInitialization(newServer);
connection.initialized();
Expand Down Expand Up @@ -523,13 +529,25 @@ export default class AutoLanguageClient {
}

this.definitions = this.definitions || new DefinitionAdapter();
return this.definitions.getDefinition(
const queryPromise = this.definitions.getDefinition(
server.connection,
server.capabilities,
this.getLanguageName(),
editor,
point,
);

if (this.serversSupportDefinitionDestinations()) {
queryPromise.then((query) => {
if (query) {
for (const def of query.definitions) {
server.considerDefinitionPath(def.path);
}
}
});
}

return queryPromise;
}

// Outline View via LS documentSymbol---------------------------------
Expand Down Expand Up @@ -786,6 +804,16 @@ export default class AutoLanguageClient {
stderr.split('\n').filter((l) => l).forEach((line) => this.logger.warn(`stderr ${line}`));
}

/**
* Indicates that the language server can support LSP functionality for
* out of project files indicated by `textDocument/definition` responses.
*
* Default: false
*/
protected serversSupportDefinitionDestinations(): boolean {
return false;
}

private getServerAdapter<T extends keyof ServerAdapters>(
server: ActiveServer, adapter: T,
): ServerAdapters[T] | undefined {
Expand Down
14 changes: 13 additions & 1 deletion lib/server-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export interface ActiveServer {
process: LanguageServerProcess;
connection: ls.LanguageClientConnection;
capabilities: ls.ServerCapabilities;
// Out of project directories that this server can also support.
additionalPaths: Set<string>;
// Considers a path from `textDocument/definition` for inclusion in `additionalPaths`.
considerDefinitionPath(path: string): void;
}

interface RestartCounter {
Expand Down Expand Up @@ -259,7 +263,15 @@ export class ServerManager {
if (filePath == null) {
return null;
}
return this._normalizedProjectPaths.find((d) => filePath.startsWith(d)) || null;

const projectPath = this._normalizedProjectPaths.find((d) => filePath.startsWith(d));
if (projectPath) {
return projectPath;
}

const serverWithClaim = this._activeServers
.find((s) => s.additionalPaths.has(path.dirname(filePath)));
return serverWithClaim && this.normalizePath(serverWithClaim.projectPath) || null;
}

public updateNormalizedProjectPaths(): void {
Expand Down
2 changes: 2 additions & 0 deletions test/adapters/autocomplete-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ describe('AutoCompleteAdapter', () => {
disposable: new CompositeDisposable(),
process: undefined as any,
projectPath: '/',
additionalPaths: new Set(),
considerDefinitionPath: (_: string): void => {},
};
}

Expand Down