src/vs/workbench/contrib/search/common/search.ts
253 LOC · 161 covered · 92 uncovered · 20 ranges · 13 concepts · 8 introducers · 15 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
search.ts ×8
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { onUnexpectedExternalError } from '../../../../base/common/errors.js';
import { IDisposable } from '../../../../base/common/lifecycle.js';
import { ISearchConfiguration, ISearchConfigurationProperties } from '../../../services/search/common/search.js';
import { SymbolKind, Location, ProviderResult, SymbolTag } from '../../../../editor/common/languages.js';
import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js';
import { URI } from '../../../../base/common/uri.js';
import { EditorResourceAccessor, SideBySideEditor } from '../../../common/editor.js';
import { IEditorService } from '../../../services/editor/common/editorService.js';
import { CancellationToken } from '../../../../base/common/cancellation.js';
import { ServicesAccessor } from '../../../../platform/instantiation/common/instantiation.js';
import { IFileService } from '../../../../platform/files/common/files.js';
import { IRange, Range } from '../../../../editor/common/core/range.js';
import { isNumber } from '../../../../base/common/types.js';
import { RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
import { compare } from '../../../../base/common/strings.js';
import { groupBy } from '../../../../base/common/arrays.js';
import * as nls from '../../../../nls.js';
import type { IConfigurationNode } from '../../../../platform/configuration/common/configurationRegistry.js';
export interface IWorkspaceSymbol {
name: string;
containerName?: string;
kind: SymbolKind;
tags?: SymbolTag[];
location: Location;
}
export interface IWorkspaceSymbolProvider {
provideWorkspaceSymbols(search: string, token: CancellationToken): ProviderResult<IWorkspaceSymbol[]>;
resolveWorkspaceSymbol?(item: IWorkspaceSymbol, token: CancellationToken): ProviderResult<IWorkspaceSymbol>;
}
export namespace WorkspaceSymbolProviderRegistry {
const _supports: IWorkspaceSymbolProvider[] = [];
export function register(provider: IWorkspaceSymbolProvider): IDisposable {
let support: IWorkspaceSymbolProvider | undefined = provider;
if (support) {
_supports.push(support);
}
return {
dispose() {
if (support) {
const idx = _supports.indexOf(support);
if (idx >= 0) {
_supports.splice(idx, 1);
support = undefined;
}
}
}
};
}
export function all(): IWorkspaceSymbolProvider[] {
return _supports.slice(0);
}
export class WorkspaceSymbolItem {
constructor(readonly symbol: IWorkspaceSymbol, readonly provider: IWorkspaceSymbolProvider) { }
}
export async function getWorkspaceSymbols(query: string, token: CancellationToken = CancellationToken.None): Promise<WorkspaceSymbolItem[]> {
const all: WorkspaceSymbolItem[] = [];
const promises = WorkspaceSymbolProviderRegistry.all().map(async provider => {
try {
const value = await provider.provideWorkspaceSymbols(query, token);
if (!value) {
return;
}
for (const symbol of value) {
all.push(new WorkspaceSymbolItem(symbol, provider));
}
} catch (err) {
onUnexpectedExternalError(err);
}
});
await Promise.all(promises);
if (token.isCancellationRequested) {
return [];
}
// de-duplicate entries
function compareItems(a: WorkspaceSymbolItem, b: WorkspaceSymbolItem): number {
let res = compare(a.symbol.name, b.symbol.name);
if (res === 0) {
res = a.symbol.kind - b.symbol.kind;
}
if (res === 0) {
res = compare(a.symbol.location.uri.toString(), b.symbol.location.uri.toString());
}
if (res === 0) {
if (a.symbol.location.range && b.symbol.location.range) {
if (!Range.areIntersecting(a.symbol.location.range, b.symbol.location.range)) {
res = Range.compareRangesUsingStarts(a.symbol.location.range, b.symbol.location.range);
}
} else if (a.provider.resolveWorkspaceSymbol && !b.provider.resolveWorkspaceSymbol) {
res = -1;
} else if (!a.provider.resolveWorkspaceSymbol && b.provider.resolveWorkspaceSymbol) {
res = 1;
}
}
if (res === 0) {
res = compare(a.symbol.containerName ?? '', b.symbol.containerName ?? '');
}
return res;
}
return groupBy(all, compareItems).map(group => group[0]).flat();
}
export interface IWorkbenchSearchConfigurationProperties extends ISearchConfigurationProperties {
quickOpen?: {
includeSymbols?: boolean;
includeHistory?: boolean;
history?: {
filterSortOrder?: 'default' | 'recency';
};
};
}
export interface IWorkbenchSearchConfiguration extends ISearchConfiguration {
search: IWorkbenchSearchConfigurationProperties;
}
/**
* Helper to return all opened editors with resources not belonging to the currently opened workspace.
*/
export function getOutOfWorkspaceEditorResources(accessor: ServicesAccessor): URI[] {
const editorService = accessor.get(IEditorService);
const contextService = accessor.get(IWorkspaceContextService);
const fileService = accessor.get(IFileService);
const resources = editorService.editors
.map(editor => EditorResourceAccessor.getOriginalUri(editor, { supportSideBySide: SideBySideEditor.PRIMARY }))
.filter(resource => !!resource && !contextService.isInsideWorkspace(resource) && fileService.hasProvider(resource));
return resources as URI[];
}
// Supports patterns of <path><#|:|(><line><#|:|,><col?>> optionally followed by a range suffix <-<endLine><#|:|,><endCol?>>
const LINE_COLON_PATTERN = /\s?[#:\(](?:line )?(\d*)(?:[#:,](\d*))?(?:-(\d*)(?:[#:,](\d*))?)?\)?:?\s*$/;
export interface IFilterAndRange {
filter: string;
range: IRange;
}
export function extractRangeFromFilter(filter: string, unless?: string[]): IFilterAndRange | undefined {
// Ignore when the unless character not the first character or is before the line colon pattern
if (!filter || unless?.some(value => {
return unlessCharPos === 0 || unlessCharPos > 0 && !LINE_COLON_PATTERN.test(filter.substring(unlessCharPos + 1));
}
let range: IRange | undefined = undefined;
// Find Line/Column number from search value using RegExp
const patternMatch = LINE_COLON_PATTERN.exec(filter);
if (patternMatch) {
// Line Number
if (isNumber(startLineNumber)) {
range = {
startLineNumber: startLineNumber,
startColumn: 1,
endLineNumber: startLineNumber,
endColumn: 1
};
// Column Number
const startColumn = parseInt(patternMatch[2] ?? '', 10);
if (isNumber(startColumn)) {
startLineNumber: range.startLineNumber,
startColumn: startColumn,
endLineNumber: range.endLineNumber,
endColumn: startColumn
};
}
// End Line Number (range selection, e.g. "20-40")
const endLineNumber = parseInt(patternMatch[3] ?? '', 10);
if (isNumber(endLineNumber)) {
// End Column Number (e.g. "20:3-40:5"), defaults to the start of the end line
const endColumn = parseInt(patternMatch[4] ?? '', 10);
range = {
startLineNumber: range.startLineNumber,
startColumn: range.startColumn,
endLineNumber: endLineNumber,
endColumn: isNumber(endColumn) ? endColumn : 1
};
}
// User has typed "something:" or "something#" without a line number, in this case treat as start of file
else if (patternMatch[1] === '') {
range = {
startLineNumber: 1,
startColumn: 1,
endLineNumber: 1,
endColumn: 1
};
}
filter: filter.substr(0, patternMatch.index), // clear range suffix from search value
range
};
}
return undefined;
}
export enum SearchUIState {
Idle,
Searching,
SlowSearch
}
export const SearchStateKey = new RawContextKey<SearchUIState>('searchState', SearchUIState.Idle);
export interface NotebookPriorityInfo {
isFromSettings: boolean;
filenamePatterns: string[];
}
export const searchConfigurationNode: IConfigurationNode = {
id: 'search',
order: 13,
title: nls.localize('searchConfigurationTitle', "Search"),
type: 'object',
properties: {}
};