提交 542b0145 编写于 作者: J Jackson Kearl

Dim the line numbers of context results

上级 70fb5c6c
...@@ -13,8 +13,26 @@ const DIRECTIVES = ['# Query:', '# Flags:', '# Including:', '# Excluding:', '# C ...@@ -13,8 +13,26 @@ const DIRECTIVES = ['# Query:', '# Flags:', '# Including:', '# Excluding:', '# C
const FLAGS = ['RegExp', 'CaseSensitive', 'IgnoreExcludeSettings', 'WordMatch']; const FLAGS = ['RegExp', 'CaseSensitive', 'IgnoreExcludeSettings', 'WordMatch'];
let cachedLastParse: { version: number, parse: ParsedSearchResults, uri: vscode.Uri } | undefined; let cachedLastParse: { version: number, parse: ParsedSearchResults, uri: vscode.Uri } | undefined;
let documentChangeListener: vscode.Disposable | undefined;
export function activate(context: vscode.ExtensionContext) { export function activate(context: vscode.ExtensionContext) {
const contextLineDecorations = vscode.window.createTextEditorDecorationType({ opacity: '0.7' });
const matchLineDecorations = vscode.window.createTextEditorDecorationType({ fontWeight: 'bold' });
const decorate = (editor: vscode.TextEditor) => {
const parsed = parseSearchResults(editor.document).filter(isResultLine);
const contextRanges = parsed.filter(line => line.isContext).map(line => line.prefixRange);
const matchRanges = parsed.filter(line => !line.isContext).map(line => line.prefixRange);
editor.setDecorations(contextLineDecorations, contextRanges);
editor.setDecorations(matchLineDecorations, matchRanges);
};
if (vscode.window.activeTextEditor && vscode.window.activeTextEditor.document.languageId === 'search-result') {
decorate(vscode.window.activeTextEditor);
}
context.subscriptions.push( context.subscriptions.push(
vscode.commands.registerCommand('searchResult.rerunSearch', () => vscode.commands.executeCommand('search.action.rerunEditorSearch')), vscode.commands.registerCommand('searchResult.rerunSearch', () => vscode.commands.executeCommand('search.action.rerunEditorSearch')),
vscode.commands.registerCommand('searchResult.rerunSearchWithContext', () => vscode.commands.executeCommand('search.action.rerunEditorSearchWithContext')), vscode.commands.registerCommand('searchResult.rerunSearchWithContext', () => vscode.commands.executeCommand('search.action.rerunEditorSearchWithContext')),
...@@ -84,15 +102,24 @@ export function activate(context: vscode.ExtensionContext) { ...@@ -84,15 +102,24 @@ export function activate(context: vscode.ExtensionContext) {
} }
}), }),
vscode.window.onDidChangeActiveTextEditor(e => { vscode.window.onDidChangeActiveTextEditor(editor => {
if (e?.document.languageId === 'search-result') { if (editor?.document.languageId === 'search-result') {
// Clear the parse whenever we open a new editor. // Clear the parse whenever we open a new editor.
// Conservative because things like the URI might remain constant even if the contents change, and re-parsing even large files is relatively fast. // Conservative because things like the URI might remain constant even if the contents change, and re-parsing even large files is relatively fast.
cachedLastParse = undefined; cachedLastParse = undefined;
documentChangeListener?.dispose();
documentChangeListener = vscode.workspace.onDidChangeTextDocument(doc => {
if (doc.document.uri === editor.document.uri) {
decorate(editor);
}
});
decorate(editor);
} }
}), }),
{ dispose() { cachedLastParse = undefined; } } { dispose() { cachedLastParse = undefined; documentChangeListener?.dispose(); } }
); );
} }
...@@ -129,12 +156,13 @@ function relativePathToUri(path: string, resultsUri: vscode.Uri): vscode.Uri | u ...@@ -129,12 +156,13 @@ function relativePathToUri(path: string, resultsUri: vscode.Uri): vscode.Uri | u
} }
type ParsedSearchFileLine = { type: 'file', location: vscode.LocationLink, allLocations: vscode.LocationLink[], path: string }; type ParsedSearchFileLine = { type: 'file', location: vscode.LocationLink, allLocations: vscode.LocationLink[], path: string };
type ParsedSearchResultLine = { type: 'result', location: vscode.LocationLink }; type ParsedSearchResultLine = { type: 'result', location: vscode.LocationLink, isContext: boolean, prefixRange: vscode.Range };
type ParsedSearchResults = Array<ParsedSearchFileLine | ParsedSearchResultLine>; type ParsedSearchResults = Array<ParsedSearchFileLine | ParsedSearchResultLine>;
const isFileLine = (line: ParsedSearchResultLine | ParsedSearchFileLine): line is ParsedSearchFileLine => line.type === 'file'; const isFileLine = (line: ParsedSearchResultLine | ParsedSearchFileLine): line is ParsedSearchFileLine => line.type === 'file';
const isResultLine = (line: ParsedSearchResultLine | ParsedSearchFileLine): line is ParsedSearchResultLine => line.type === 'result';
function parseSearchResults(document: vscode.TextDocument, token: vscode.CancellationToken): ParsedSearchResults { function parseSearchResults(document: vscode.TextDocument, token?: vscode.CancellationToken): ParsedSearchResults {
if (cachedLastParse && cachedLastParse.uri === document.uri && cachedLastParse.version === document.version) { if (cachedLastParse && cachedLastParse.uri === document.uri && cachedLastParse.version === document.version) {
return cachedLastParse.parse; return cachedLastParse.parse;
...@@ -147,7 +175,8 @@ function parseSearchResults(document: vscode.TextDocument, token: vscode.Cancell ...@@ -147,7 +175,8 @@ function parseSearchResults(document: vscode.TextDocument, token: vscode.Cancell
let currentTargetLocations: vscode.LocationLink[] | undefined = undefined; let currentTargetLocations: vscode.LocationLink[] | undefined = undefined;
for (let i = 0; i < lines.length; i++) { for (let i = 0; i < lines.length; i++) {
if (token.isCancellationRequested) { return []; } // TODO: This is probably always false, given we're pegging the thread...
if (token?.isCancellationRequested) { return []; }
const line = lines[i]; const line = lines[i];
const fileLine = FILE_LINE_REGEX.exec(line); const fileLine = FILE_LINE_REGEX.exec(line);
...@@ -186,7 +215,7 @@ function parseSearchResults(document: vscode.TextDocument, token: vscode.Cancell ...@@ -186,7 +215,7 @@ function parseSearchResults(document: vscode.TextDocument, token: vscode.Cancell
currentTargetLocations?.push(location); currentTargetLocations?.push(location);
links[i] = { type: 'result', location }; links[i] = { type: 'result', location, isContext: seperator === ' ', prefixRange: new vscode.Range(i, 0, i, metadataOffset) };
} }
} }
......
...@@ -114,6 +114,13 @@ ...@@ -114,6 +114,13 @@
"settings": { "settings": {
"foreground": "#CE9178" "foreground": "#CE9178"
} }
},
{
"name": "HC Search Editor context line override",
"scope": "meta.resultLinePrefix.contextLinePrefix.search",
"settings": {
"foreground": "#CBEDCB",
}
} }
] ]
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册