From 542b0145b3244f3a24b662240574d7b57fc3a4d7 Mon Sep 17 00:00:00 2001 From: Jackson Kearl Date: Wed, 11 Dec 2019 16:31:05 -0800 Subject: [PATCH] Dim the line numbers of context results --- extensions/search-result/src/extension.ts | 43 ++++++++++++++++--- .../theme-defaults/themes/hc_black.json | 7 +++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/extensions/search-result/src/extension.ts b/extensions/search-result/src/extension.ts index 0c5c85bfe60..0bd5e12188e 100644 --- a/extensions/search-result/src/extension.ts +++ b/extensions/search-result/src/extension.ts @@ -13,8 +13,26 @@ const DIRECTIVES = ['# Query:', '# Flags:', '# Including:', '# Excluding:', '# C const FLAGS = ['RegExp', 'CaseSensitive', 'IgnoreExcludeSettings', 'WordMatch']; let cachedLastParse: { version: number, parse: ParsedSearchResults, uri: vscode.Uri } | undefined; +let documentChangeListener: vscode.Disposable | undefined; + 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( vscode.commands.registerCommand('searchResult.rerunSearch', () => vscode.commands.executeCommand('search.action.rerunEditorSearch')), vscode.commands.registerCommand('searchResult.rerunSearchWithContext', () => vscode.commands.executeCommand('search.action.rerunEditorSearchWithContext')), @@ -84,15 +102,24 @@ export function activate(context: vscode.ExtensionContext) { } }), - vscode.window.onDidChangeActiveTextEditor(e => { - if (e?.document.languageId === 'search-result') { + vscode.window.onDidChangeActiveTextEditor(editor => { + if (editor?.document.languageId === 'search-result') { // 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. 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 } 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; 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) { return cachedLastParse.parse; @@ -147,7 +175,8 @@ function parseSearchResults(document: vscode.TextDocument, token: vscode.Cancell let currentTargetLocations: vscode.LocationLink[] | undefined = undefined; 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 fileLine = FILE_LINE_REGEX.exec(line); @@ -186,7 +215,7 @@ function parseSearchResults(document: vscode.TextDocument, token: vscode.Cancell currentTargetLocations?.push(location); - links[i] = { type: 'result', location }; + links[i] = { type: 'result', location, isContext: seperator === ' ', prefixRange: new vscode.Range(i, 0, i, metadataOffset) }; } } diff --git a/extensions/theme-defaults/themes/hc_black.json b/extensions/theme-defaults/themes/hc_black.json index f76d7bb960f..73309c34b44 100644 --- a/extensions/theme-defaults/themes/hc_black.json +++ b/extensions/theme-defaults/themes/hc_black.json @@ -114,6 +114,13 @@ "settings": { "foreground": "#CE9178" } + }, + { + "name": "HC Search Editor context line override", + "scope": "meta.resultLinePrefix.contextLinePrefix.search", + "settings": { + "foreground": "#CBEDCB", + } } ] } -- GitLab