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

add fill selection text for quick search #191956

Merged
merged 14 commits into from
Sep 14, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { Action2, registerAction2 } from 'vs/platform/actions/common/actions';
import { category } from 'vs/workbench/contrib/search/browser/searchActionsBase';
import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
import { TEXT_SEARCH_QUICK_ACCESS_PREFIX } from 'vs/workbench/contrib/search/browser/quickTextSearch/textSearchQuickAccess';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IEditor } from 'vs/editor/common/editorCommon';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { getSelectionTextFromEditor } from 'vs/workbench/contrib/search/browser/searchView';

registerAction2(class TextSearchQuickAccessAction extends Action2 {

Expand All @@ -29,6 +33,28 @@ registerAction2(class TextSearchQuickAccessAction extends Action2 {

override async run(accessor: ServicesAccessor, match: RenderableMatch | undefined): Promise<any> {
const quickInputService = accessor.get(IQuickInputService);
quickInputService.quickAccess.show(TEXT_SEARCH_QUICK_ACCESS_PREFIX);
const searchText = getSearchText(accessor) ?? '';
weartist marked this conversation as resolved.
Show resolved Hide resolved
quickInputService.quickAccess.show(TEXT_SEARCH_QUICK_ACCESS_PREFIX + searchText);
}
});

function getSearchText(accessor: ServicesAccessor): string | null {
const editorService = accessor.get(IEditorService);
const configurationService = accessor.get(IConfigurationService);

const activeEditor: IEditor = editorService.activeTextEditorControl as IEditor;
if (!activeEditor) {
weartist marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
if (!activeEditor.hasTextFocus()) {
return null;
}

// only happen if it would also happen for the search view
const seedSearchStringFromSelection = configurationService.getValue<boolean>('editor.find.seedSearchStringFromSelection');
if (!seedSearchStringFromSelection) {
return null;
}

return getSelectionTextFromEditor(false, activeEditor);
}
80 changes: 44 additions & 36 deletions src/vs/workbench/contrib/search/browser/searchView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1288,45 +1288,12 @@ export class SearchView extends ViewPane {
}
}

if (!isCodeEditor(editor) || !editor.hasModel()) {
if (!editor) {
return null;
}

const range = editor.getSelection();
if (!range) {
return null;
}

if (range.isEmpty() && this.searchConfig.seedWithNearestWord && allowUnselectedWord) {
const wordAtPosition = editor.getModel().getWordAtPosition(range.getStartPosition());
if (wordAtPosition) {
return wordAtPosition.word;
}
}

if (!range.isEmpty()) {
let searchText = '';
for (let i = range.startLineNumber; i <= range.endLineNumber; i++) {
let lineText = editor.getModel().getLineContent(i);
if (i === range.endLineNumber) {
lineText = lineText.substring(0, range.endColumn - 1);
}

if (i === range.startLineNumber) {
lineText = lineText.substring(range.startColumn - 1);
}

if (i !== range.startLineNumber) {
lineText = '\n' + lineText;
}

searchText += lineText;
}

return searchText;
}

return null;
const allowUnselected = this.searchConfig.seedWithNearestWord && allowUnselectedWord;
return getSelectionTextFromEditor(allowUnselected, editor);
}

private showsFileTypes(): boolean {
Expand Down Expand Up @@ -2175,3 +2142,44 @@ export function getEditorSelectionFromMatch(element: FileMatchOrMatch, viewModel
}
return undefined;
}

export function getSelectionTextFromEditor(allowUnselectedWord: boolean, editor: IEditor): string | null {

if (!isCodeEditor(editor) || !editor.hasModel()) {
return null;
}

const range = editor.getSelection();
if (!range) {
return null;
}

if (range.isEmpty()) {
if (allowUnselectedWord) {
const wordAtPosition = editor.getModel().getWordAtPosition(range.getStartPosition());
return wordAtPosition?.word ?? null;
} else {
return null;
}
}

let searchText = '';
for (let i = range.startLineNumber; i <= range.endLineNumber; i++) {
let lineText = editor.getModel().getLineContent(i);
if (i === range.endLineNumber) {
lineText = lineText.substring(0, range.endColumn - 1);
}

if (i === range.startLineNumber) {
lineText = lineText.substring(range.startColumn - 1);
}

if (i !== range.startLineNumber) {
lineText = '\n' + lineText;
}

searchText += lineText;
}

return searchText;
}