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

Go to References - Where Imports Used #1491

Merged
merged 18 commits into from May 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/language-server/src/ls-config.ts
Expand Up @@ -14,6 +14,7 @@ const defaultLSConfig: LSConfig = {
completions: { enable: true },
definitions: { enable: true },
findReferences: { enable: true },
fileReferences: { enable: true },
documentSymbols: { enable: true },
codeActions: { enable: true },
rename: { enable: true },
Expand Down Expand Up @@ -97,6 +98,9 @@ export interface LSTypescriptConfig {
findReferences: {
enable: boolean;
};
fileReferences: {
enable: boolean;
};
definitions: {
enable: boolean;
};
Expand Down
9 changes: 9 additions & 0 deletions packages/language-server/src/plugins/PluginHost.ts
Expand Up @@ -389,6 +389,15 @@ export class PluginHost implements LSProvider, OnWatchFileChanges {
);
}

async fileReferences(fileName: string): Promise<Location[] | null> {
return await this.execute<any>(
'fileReferences',
[fileName],
ExecuteMode.FirstNonNull,
'high'
);
}

async getSignatureHelp(
textDocument: TextDocumentIdentifier,
position: Position,
Expand Down
5 changes: 5 additions & 0 deletions packages/language-server/src/plugins/interfaces.ts
Expand Up @@ -143,6 +143,10 @@ export interface FindReferencesProvider {
): Promise<Location[] | null>;
}

export interface FileReferencesProvider {
fileReferences(fileName: string): Promise<Location[] | null>;
}

export interface SignatureHelpProvider {
getSignatureHelp(
document: Document,
Expand Down Expand Up @@ -199,6 +203,7 @@ type ProviderBase = DiagnosticsProvider &
UpdateImportsProvider &
CodeActionsProvider &
FindReferencesProvider &
FileReferencesProvider &
RenameProvider &
SignatureHelpProvider &
SemanticTokensProvider &
Expand Down
Expand Up @@ -36,6 +36,7 @@ import {
DocumentSymbolsProvider,
FileRename,
FindReferencesProvider,
FileReferencesProvider,
HoverProvider,
ImplementationProvider,
OnWatchFileChanges,
Expand All @@ -54,6 +55,7 @@ import {
CompletionsProviderImpl
} from './features/CompletionProvider';
import { DiagnosticsProviderImpl } from './features/DiagnosticsProvider';
import { FindFileReferencesProviderImpl } from './features/FindFileReferencesProvider';
import { FindReferencesProviderImpl } from './features/FindReferencesProvider';
import { getDirectiveCommentCompletions } from './features/getDirectiveCommentCompletions';
import { HoverProviderImpl } from './features/HoverProvider';
Expand Down Expand Up @@ -85,6 +87,7 @@ export class TypeScriptPlugin
UpdateImportsProvider,
RenameProvider,
FindReferencesProvider,
FileReferencesProvider,
SelectionRangeProvider,
SignatureHelpProvider,
SemanticTokensProvider,
Expand All @@ -104,6 +107,8 @@ export class TypeScriptPlugin
private readonly renameProvider: RenameProviderImpl;
private readonly hoverProvider: HoverProviderImpl;
private readonly findReferencesProvider: FindReferencesProviderImpl;
private readonly findFileReferencesProvider: FindFileReferencesProviderImpl;

private readonly selectionRangeProvider: SelectionRangeProviderImpl;
private readonly signatureHelpProvider: SignatureHelpProviderImpl;
private readonly semanticTokensProvider: SemanticTokensProviderImpl;
Expand All @@ -130,6 +135,9 @@ export class TypeScriptPlugin
this.renameProvider = new RenameProviderImpl(this.lsAndTsDocResolver, configManager);
this.hoverProvider = new HoverProviderImpl(this.lsAndTsDocResolver);
this.findReferencesProvider = new FindReferencesProviderImpl(this.lsAndTsDocResolver);
this.findFileReferencesProvider = new FindFileReferencesProviderImpl(
this.lsAndTsDocResolver
);
this.selectionRangeProvider = new SelectionRangeProviderImpl(this.lsAndTsDocResolver);
this.signatureHelpProvider = new SignatureHelpProviderImpl(this.lsAndTsDocResolver);
this.semanticTokensProvider = new SemanticTokensProviderImpl(this.lsAndTsDocResolver);
Expand Down Expand Up @@ -423,6 +431,14 @@ export class TypeScriptPlugin
return this.findReferencesProvider.findReferences(document, position, context);
}

async fileReferences(fileName: string): Promise<Location[] | null> {
if (!this.featureEnabled('fileReferences')) {
return null;
}

return this.findFileReferencesProvider.fileReferences(fileName);
}

async onWatchFileChanges(onWatchFileChangesParas: OnWatchFileChangesPara[]): Promise<void> {
let doneUpdateProjectFiles = false;

Expand Down
@@ -0,0 +1,40 @@
import { Location } from 'vscode-languageserver';
import { FileReferencesProvider } from '../../interfaces';
import { LSAndTSDocResolver } from '../LSAndTSDocResolver';

export class FindFileReferencesProviderImpl implements FileReferencesProvider {
constructor(private readonly lsAndTsDocResolver: LSAndTSDocResolver) {}

async fileReferences(fileName: string): Promise<Location[] | null> {
Jojoshua marked this conversation as resolved.
Show resolved Hide resolved
const lang = await this.getLSForPath(fileName);

const references = lang.getFileReferences(fileName);

if (!references) {
return null;
}

return [];

// const docs = new SnapshotFragmentMap(this.lsAndTsDocResolver);
// docs.set(tsDoc.filePath, { fragment, snapshot: tsDoc });

// const locations = await Promise.all(
// references.map(async (ref) => {
// const defDoc = await docs.retrieveFragment(ref.fileName);

// return Location.create(
// pathToUrl(ref.fileName),
// convertToLocationRange(defDoc, ref.textSpan)
// );
// })
// );
// // Some references are in generated code but not wrapped with explicit ignore comments.
// // These show up as zero-length ranges, so filter them out.
// return locations.filter(hasNonZeroRange);
}

private async getLSForPath(path: string) {
return this.lsAndTsDocResolver.getLSForPath(path);
}
}
17 changes: 7 additions & 10 deletions packages/language-server/src/server.ts
Expand Up @@ -161,9 +161,6 @@ export function startServer(options?: LSOptions) {
);

const clientSupportApplyEditCommand = !!evt.capabilities.workspace?.applyEdit;
const clientCodeActionCapabilities = evt.capabilities.textDocument?.codeAction;
const clientSupportedCodeActionKinds =
clientCodeActionCapabilities?.codeActionLiteralSupport?.codeActionKind.valueSet;

return {
capabilities: {
Expand Down Expand Up @@ -210,19 +207,15 @@ export function startServer(options?: LSOptions) {
colorProvider: true,
documentSymbolProvider: true,
definitionProvider: true,
codeActionProvider: clientCodeActionCapabilities?.codeActionLiteralSupport
codeActionProvider: evt.capabilities.textDocument?.codeAction
?.codeActionLiteralSupport
? {
codeActionKinds: [
CodeActionKind.QuickFix,
CodeActionKind.SourceOrganizeImports,
SORT_IMPORT_CODE_ACTION_KIND,
...(clientSupportApplyEditCommand ? [CodeActionKind.Refactor] : [])
].filter(
clientSupportedCodeActionKinds &&
evt.initializationOptions.shouldFilterCodeActionKind
? (kind) => clientSupportedCodeActionKinds.includes(kind)
: () => true
)
]
Jojoshua marked this conversation as resolved.
Show resolved Hide resolved
}
: true,
executeCommandProvider: clientSupportApplyEditCommand
Expand Down Expand Up @@ -426,6 +419,10 @@ export function startServer(options?: LSOptions) {
pluginHost.updateImports(fileRename)
);

connection.onRequest('$/getFileReferences', async (fileName: string) => {
return pluginHost.fileReferences(fileName);
});

connection.onRequest('$/getCompiledCode', async (uri: DocumentUri) => {
const doc = docManager.get(uri);
if (!doc) {
Expand Down
10 changes: 10 additions & 0 deletions packages/svelte-vscode/src/extension.ts
Expand Up @@ -223,6 +223,7 @@ export function activateSvelteLanguageServer(context: ExtensionContext) {

addDidChangeTextDocumentListener(getLS);

addWhereImportsUsedListener(getLS);
addRenameFileListener(getLS);

addCompilePreviewCommand(getLS, context);
Expand Down Expand Up @@ -323,6 +324,15 @@ function addDidChangeTextDocumentListener(getLS: () => LanguageClient) {
});
}

async function addWhereImportsUsedListener(getLS: () => LanguageClient) {
//dummy test handler
workspace.onWillSaveTextDocument(async (evt) => {
const fileName = evt.document.fileName;

await getLS().sendRequest<LSWorkspaceEdit | null>('$/getFileReferences', fileName);
});
}

function addRenameFileListener(getLS: () => LanguageClient) {
workspace.onDidRenameFiles(async (evt) => {
const oldUri = evt.files[0].oldUri.toString(true);
Expand Down