提交 dbe26fa7 编写于 作者: P Peng Lyu 提交者: GitHub

Merge pull request #30021 from mihailik/master

Ctrl-T to use word under cursor or current selection
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as editorCommon from 'vs/editor/common/editorCommon';
export function getSelectionSearchString(editor: editorCommon.ICommonCodeEditor): string {
let selection = editor.getSelection();
// if selection spans multiple lines, default search string to empty
if (selection.startLineNumber === selection.endLineNumber) {
if (selection.isEmpty()) {
let wordAtPosition = editor.getModel().getWordAtPosition(selection.getStartPosition());
if (wordAtPosition) {
return wordAtPosition.word;
}
} else {
return editor.getModel().getValueInRange(selection);
}
}
return null;
}
......@@ -16,6 +16,7 @@ import * as editorCommon from 'vs/editor/common/editorCommon';
import { editorAction, commonEditorContribution, ServicesAccessor, EditorAction, EditorCommand, CommonEditorRegistry } from 'vs/editor/common/editorCommonExtensions';
import { FIND_IDS, FindModelBoundToEditorModel, ToggleCaseSensitiveKeybinding, ToggleRegexKeybinding, ToggleWholeWordKeybinding, ToggleSearchScopeKeybinding, ShowPreviousFindTermKeybinding, ShowNextFindTermKeybinding } from 'vs/editor/contrib/find/common/findModel';
import { FindReplaceState, FindReplaceStateChangedEvent, INewFindReplaceState } from 'vs/editor/contrib/find/common/findState';
import { getSelectionSearchString } from 'vs/editor/contrib/find/common/find';
import { DocumentHighlightProviderRegistry } from 'vs/editor/common/modes';
import { RunOnceScheduler, Delayer } from 'vs/base/common/async';
import { CursorChangeReason, ICursorSelectionChangedEvent } from 'vs/editor/common/controller/cursorEvents';
......@@ -206,20 +207,7 @@ export class CommonFindController extends Disposable implements editorCommon.IEd
}
public getSelectionSearchString(): string {
let selection = this._editor.getSelection();
if (selection.startLineNumber === selection.endLineNumber) {
if (selection.isEmpty()) {
let wordAtPosition = this._editor.getModel().getWordAtPosition(selection.getStartPosition());
if (wordAtPosition) {
return wordAtPosition.word;
}
} else {
return this._editor.getModel().getValueInRange(selection);
}
}
return null;
return getSelectionSearchString(this._editor);
}
protected _start(opts: IFindStartOptions): void {
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
import {
getSelectionSearchString
} from 'vs/editor/contrib/find/common/find';
import { withMockCodeEditor } from 'vs/editor/test/common/mocks/mockCodeEditor';
suite('Find', () => {
test('search string at position', () => {
withMockCodeEditor([
'ABC DEF',
'0123 456'
], {}, (editor, cursor) => {
// The cursor is at the very top, of the file, at the first ABC
let searchStringAtTop = getSelectionSearchString(editor);
assert.equal(searchStringAtTop, 'ABC');
// Move cursor to the end of ABC
editor.setPosition(new Position(1, 3));
let searchStringAfterABC = getSelectionSearchString(editor);
assert.equal(searchStringAfterABC, 'ABC');
// Move cursor to DEF
editor.setPosition(new Position(1, 5));
let searchStringInsideDEF = getSelectionSearchString(editor);
assert.equal(searchStringInsideDEF, 'DEF');
});
});
test('search string with selection', () => {
withMockCodeEditor([
'ABC DEF',
'0123 456'
], {}, (editor, cursor) => {
// Select A of ABC
editor.setSelection(new Range(1, 1, 1, 2));
let searchStringSelectionA = getSelectionSearchString(editor);
assert.equal(searchStringSelectionA, 'A');
// Select BC of ABC
editor.setSelection(new Range(1, 2, 1, 4));
let searchStringSelectionBC = getSelectionSearchString(editor);
assert.equal(searchStringSelectionBC, 'BC');
// Select BC DE
editor.setSelection(new Range(1, 2, 1, 7));
let searchStringSelectionBCDE = getSelectionSearchString(editor);
assert.equal(searchStringSelectionBCDE, 'BC DE');
});
});
test('search string with multiline selection', () => {
withMockCodeEditor([
'ABC DEF',
'0123 456'
], {}, (editor, cursor) => {
// Select first line and newline
editor.setSelection(new Range(1, 1, 2, 1));
let searchStringSelectionWholeLine = getSelectionSearchString(editor);
assert.equal(searchStringSelectionWholeLine, null);
// Select first line and chunk of second
editor.setSelection(new Range(1, 1, 2, 4));
let searchStringSelectionTwoLines = getSelectionSearchString(editor);
assert.equal(searchStringSelectionTwoLines, null);
// Select end of first line newline and and chunk of second
editor.setSelection(new Range(1, 7, 2, 4));
let searchStringSelectionSpanLines = getSelectionSearchString(editor);
assert.equal(searchStringSelectionSpanLines, null);
});
});
});
\ No newline at end of file
......@@ -11,17 +11,20 @@ import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { ViewletRegistry, Extensions as ViewletExtensions, ViewletDescriptor } from 'vs/workbench/browser/viewlet';
import { IConfigurationRegistry, Extensions as ConfigurationExtensions, ConfigurationScope } from 'vs/platform/configuration/common/configurationRegistry';
import nls = require('vs/nls');
import { IAction } from 'vs/base/common/actions';
import { TPromise } from 'vs/base/common/winjs.base';
import { IAction, Action } from 'vs/base/common/actions';
import { explorerItemToFileResource } from 'vs/workbench/parts/files/common/files';
import { SyncActionDescriptor } from 'vs/platform/actions/common/actions';
import { Separator } from 'vs/base/browser/ui/actionbar/actionbar';
import { Scope, IActionBarRegistry, Extensions as ActionBarExtensions, ActionBarContributor } from 'vs/workbench/browser/actions';
import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actionRegistry';
import { QuickOpenHandlerDescriptor, IQuickOpenRegistry, Extensions as QuickOpenExtensions, QuickOpenAction } from 'vs/workbench/browser/quickopen';
import { QuickOpenHandlerDescriptor, IQuickOpenRegistry, Extensions as QuickOpenExtensions } from 'vs/workbench/browser/quickopen';
import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IQuickOpenService } from 'vs/platform/quickOpen/common/quickOpen';
import { ICodeEditorService } from 'vs/editor/common/services/codeEditorService';
import { getSelectionSearchString } from 'vs/editor/contrib/find/common/find';
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
import { KeyMod, KeyCode } from 'vs/base/common/keyCodes';
import { ITree } from 'vs/base/parts/tree/browser/tree';
......@@ -226,10 +229,30 @@ const ACTION_ID = 'workbench.action.showAllSymbols';
const ACTION_LABEL = nls.localize('showTriggerActions', "Go to Symbol in Workspace...");
const ALL_SYMBOLS_PREFIX = '#';
class ShowAllSymbolsAction extends QuickOpenAction {
class ShowAllSymbolsAction extends Action {
constructor(actionId: string, actionLabel: string, @IQuickOpenService quickOpenService: IQuickOpenService) {
super(actionId, actionLabel, ALL_SYMBOLS_PREFIX, quickOpenService);
constructor(
actionId: string, actionLabel: string,
@IQuickOpenService private quickOpenService: IQuickOpenService,
@ICodeEditorService private editorService: ICodeEditorService) {
super(actionId, actionLabel);
this.enabled = !!this.quickOpenService;
}
public run(context?: any): TPromise<void> {
let prefix = ALL_SYMBOLS_PREFIX;
let inputSelection: { start: number; end: number; } = void 0;
let editor = this.editorService.getFocusedCodeEditor();
const word = editor && getSelectionSearchString(editor);
if (word) {
prefix = prefix + word;
inputSelection = { start: 1, end: word.length + 1 };
}
this.quickOpenService.show(prefix, { inputSelection });
return TPromise.as(null);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册