Merge pull request #3619 from jakebailey/replace-occurrences

Replace occurrences with highlights
This commit is contained in:
Henning Dieterichs 2023-03-02 13:05:42 +01:00 committed by GitHub
commit 6f4b37e3fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 20 deletions

View file

@ -715,7 +715,10 @@ export class QuickInfoAdapter extends Adapter implements languages.HoverProvider
// --- occurrences ------ // --- occurrences ------
export class OccurrencesAdapter extends Adapter implements languages.DocumentHighlightProvider { export class DocumentHighlightAdapter
extends Adapter
implements languages.DocumentHighlightProvider
{
public async provideDocumentHighlights( public async provideDocumentHighlights(
model: editor.ITextModel, model: editor.ITextModel,
position: Position, position: Position,
@ -729,19 +732,24 @@ export class OccurrencesAdapter extends Adapter implements languages.DocumentHig
return; return;
} }
const entries = await worker.getOccurrencesAtPosition(resource.toString(), offset); const entries = await worker.getDocumentHighlights(resource.toString(), offset, [
resource.toString()
]);
if (!entries || model.isDisposed()) { if (!entries || model.isDisposed()) {
return; return;
} }
return entries.map((entry) => { return entries.flatMap((entry) => {
return <languages.DocumentHighlight>{ return entry.highlightSpans.map((highlightSpans) => {
range: this._textSpanToRange(model, entry.textSpan), return <languages.DocumentHighlight>{
kind: entry.isWriteAccess range: this._textSpanToRange(model, highlightSpans.textSpan),
? languages.DocumentHighlightKind.Write kind:
: languages.DocumentHighlightKind.Text highlightSpans.kind === 'writtenReference'
}; ? languages.DocumentHighlightKind.Write
: languages.DocumentHighlightKind.Text
};
});
}); });
} }
} }

View file

@ -440,13 +440,10 @@ export interface TypeScriptWorker {
*/ */
getQuickInfoAtPosition(fileName: string, position: number): Promise<any | undefined>; getQuickInfoAtPosition(fileName: string, position: number): Promise<any | undefined>;
/** getDocumentHighlights(
* Get other ranges which are related to the item at the given position in the file (often used for highlighting).
* @returns `Promise<ReadonlyArray<typescript.ReferenceEntry> | undefined>`
*/
getOccurrencesAtPosition(
fileName: string, fileName: string,
position: number position: number,
filesToSearch: string[]
): Promise<ReadonlyArray<any> | undefined>; ): Promise<ReadonlyArray<any> | undefined>;
/** /**

View file

@ -86,7 +86,7 @@ function setupMode(
providers.push( providers.push(
languages.registerDocumentHighlightProvider( languages.registerDocumentHighlightProvider(
modeId, modeId,
new languageFeatures.OccurrencesAdapter(worker) new languageFeatures.DocumentHighlightAdapter(worker)
) )
); );
} }

View file

@ -299,14 +299,15 @@ export class TypeScriptWorker implements ts.LanguageServiceHost, ITypeScriptWork
return this._languageService.getQuickInfoAtPosition(fileName, position); return this._languageService.getQuickInfoAtPosition(fileName, position);
} }
async getOccurrencesAtPosition( async getDocumentHighlights(
fileName: string, fileName: string,
position: number position: number,
): Promise<ReadonlyArray<ts.ReferenceEntry> | undefined> { filesToSearch: string[]
): Promise<ReadonlyArray<ts.DocumentHighlights> | undefined> {
if (fileNameIsLib(fileName)) { if (fileNameIsLib(fileName)) {
return undefined; return undefined;
} }
return this._languageService.getOccurrencesAtPosition(fileName, position); return this._languageService.getDocumentHighlights(fileName, position, filesToSearch);
} }
async getDefinitionAtPosition( async getDefinitionAtPosition(