-
Notifications
You must be signed in to change notification settings - Fork 36.5k
Extract autosuggest editor to own class; bring into settings #55924
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
Merged
JacksonKearl
merged 4 commits into
microsoft:master
from
JacksonKearl:settings-autosuggest
Aug 10, 2018
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
231 changes: 231 additions & 0 deletions
231
src/vs/workbench/parts/codeEditor/browser/autosuggestEnabledInput.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * 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 'vs/css!./media/autosuggestEnabledInput'; | ||
| import { $, addClass, append, removeClass, Dimension } from 'vs/base/browser/dom'; | ||
| import { chain, Emitter, Event } from 'vs/base/common/event'; | ||
| import { KeyCode } from 'vs/base/common/keyCodes'; | ||
| import { IDisposable, dispose } from 'vs/base/common/lifecycle'; | ||
| import { isMacintosh } from 'vs/base/common/platform'; | ||
| import uri from 'vs/base/common/uri'; | ||
| import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget'; | ||
| import { IEditorOptions } from 'vs/editor/common/config/editorOptions'; | ||
| import * as modes from 'vs/editor/common/modes'; | ||
| import { IModelService } from 'vs/editor/common/services/modelService'; | ||
| import { ContextMenuController } from 'vs/editor/contrib/contextmenu/contextmenu'; | ||
| import { SnippetController2 } from 'vs/editor/contrib/snippet/snippetController2'; | ||
| import { SuggestController } from 'vs/editor/contrib/suggest/suggestController'; | ||
| import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; | ||
| import { inputBackground, inputBorder, inputForeground, inputPlaceholderForeground } from 'vs/platform/theme/common/colorRegistry'; | ||
| import { IThemeService } from 'vs/platform/theme/common/themeService'; | ||
| import { Component } from 'vs/workbench/common/component'; | ||
| import { MenuPreventer } from 'vs/workbench/parts/codeEditor/browser/menuPreventer'; | ||
| import { getSimpleEditorOptions } from 'vs/workbench/parts/codeEditor/browser/simpleEditorOptions'; | ||
| import { Position } from 'vs/editor/common/core/position'; | ||
| import { Range } from 'vs/editor/common/core/range'; | ||
| import { ITextModel } from 'vs/editor/common/model'; | ||
| import { IContextKey } from 'vs/platform/contextkey/common/contextkey'; | ||
|
|
||
| interface AutosuggestResultsProvider { | ||
| /** | ||
| * Provider function for autosuggest results. | ||
| * | ||
| * @param query the full text of the input. | ||
| */ | ||
| provideResults: (query: string) => string[]; | ||
|
|
||
| /** | ||
| * Trigger characters for this input. Autocompletions will appear when one of these is typed, | ||
| * or upon `ctrl+space` triggering at a word boundary. | ||
| * | ||
| * Defaults to the empty array. | ||
| */ | ||
| triggerCharacters?: string[]; | ||
|
|
||
| /** | ||
| * Defines the sorting function used when showing results. | ||
| * | ||
| * Defaults to the identity function. | ||
| */ | ||
| sortKey?: (result: string) => string; | ||
| } | ||
|
|
||
| interface AutosuggestEnabledInputOptions { | ||
| /** | ||
| * The text to show when no input is present. | ||
| * | ||
| * Defaults to the empty string. | ||
| */ | ||
| placeholderText?: string; | ||
|
|
||
| /** | ||
| * Context key tracking the focus state of this element | ||
| */ | ||
| focusContextKey?: IContextKey<boolean>; | ||
| } | ||
|
|
||
| export class AutosuggestEnabledInput extends Component { | ||
|
|
||
| private _onShouldFocusResults = new Emitter<void>(); | ||
| readonly onShouldFocusResults: Event<void> = this._onShouldFocusResults.event; | ||
|
|
||
| private _onEnter = new Emitter<void>(); | ||
| readonly onEnter: Event<void> = this._onEnter.event; | ||
|
|
||
| private _onInputDidChange = new Emitter<string>(); | ||
| readonly onInputDidChange: Event<string> = this._onInputDidChange.event; | ||
|
|
||
| private disposables: IDisposable[] = []; | ||
| private inputWidget: CodeEditorWidget; | ||
| private stylingContainer: HTMLDivElement; | ||
| private placeholderText: HTMLDivElement; | ||
|
|
||
| constructor( | ||
| id: string, | ||
| parent: HTMLElement, | ||
| autosuggestProvider: AutosuggestResultsProvider, | ||
| ariaLabel: string, | ||
| resourceHandle: string, | ||
| options: AutosuggestEnabledInputOptions, | ||
| @IThemeService themeService: IThemeService, | ||
| @IInstantiationService instantiationService: IInstantiationService, | ||
| @IModelService modelService: IModelService, | ||
| ) { | ||
| super(id, themeService); | ||
|
|
||
| this.stylingContainer = append(parent, $('.autosuggest-input-container')); | ||
| this.placeholderText = append(this.stylingContainer, $('.autosuggest-input-placeholder', null, options.placeholderText || '')); | ||
|
|
||
| this.inputWidget = instantiationService.createInstance(CodeEditorWidget, this.stylingContainer, | ||
| mixinHTMLInputStyleOptions(getSimpleEditorOptions(), ariaLabel), | ||
| { | ||
| contributions: [SuggestController, SnippetController2, ContextMenuController, MenuPreventer], | ||
| isSimpleWidget: true, | ||
| }); | ||
|
|
||
| let scopeHandle = uri.parse(resourceHandle); | ||
| this.inputWidget.setModel(modelService.createModel('', null, scopeHandle, true)); | ||
|
|
||
| this.disposables.push(this.inputWidget.onDidPaste(() => this.setValue(this.getValue()))); // setter cleanses | ||
|
|
||
| this.disposables.push((this.inputWidget.onDidFocusEditorText(() => { | ||
| if (options.focusContextKey) { options.focusContextKey.set(true); } | ||
| addClass(this.stylingContainer, 'synthetic-focus'); | ||
| }))); | ||
| this.disposables.push((this.inputWidget.onDidBlurEditorText(() => { | ||
| if (options.focusContextKey) { options.focusContextKey.set(false); } | ||
| removeClass(this.stylingContainer, 'synthetic-focus'); | ||
| }))); | ||
|
|
||
| const onKeyDownMonaco = chain(this.inputWidget.onKeyDown); | ||
| onKeyDownMonaco.filter(e => e.keyCode === KeyCode.Enter).on(e => { e.preventDefault(); this._onEnter.fire(); }, this, this.disposables); | ||
| onKeyDownMonaco.filter(e => e.keyCode === KeyCode.DownArrow && (isMacintosh ? e.metaKey : e.ctrlKey)).on(() => this._onShouldFocusResults.fire(), this, this.disposables); | ||
|
|
||
| let preexistingContent = this.getValue(); | ||
| this.disposables.push(this.inputWidget.getModel().onDidChangeContent(() => { | ||
| let content = this.getValue(); | ||
| this.placeholderText.style.visibility = content ? 'hidden' : 'visible'; | ||
| if (preexistingContent.trim() === content.trim()) { return; } | ||
| this._onInputDidChange.fire(); | ||
| preexistingContent = content; | ||
| })); | ||
|
|
||
| let validatedAutoSuggestProvider = { | ||
| provideResults: autosuggestProvider.provideResults, | ||
| sortKey: autosuggestProvider.sortKey || (a => a), | ||
| triggerCharacters: autosuggestProvider.triggerCharacters || [] | ||
| }; | ||
|
|
||
| this.disposables.push(modes.SuggestRegistry.register({ scheme: scopeHandle.scheme, pattern: '**/' + scopeHandle.path, hasAccessToAllModels: true }, { | ||
| triggerCharacters: validatedAutoSuggestProvider.triggerCharacters, | ||
| provideCompletionItems: (model: ITextModel, position: Position, _context: modes.SuggestContext) => { | ||
| let query = model.getValue(); | ||
|
|
||
| let wordStart = query.lastIndexOf(' ', position.column - 1) + 1; | ||
| let alreadyTypedCount = position.column - wordStart - 1; | ||
|
|
||
| // dont show autosuggestions if the user has typed something, but hasn't used the trigger character | ||
| if (alreadyTypedCount > 0 && (validatedAutoSuggestProvider.triggerCharacters).indexOf(query[wordStart]) === -1) { return { suggestions: [] }; } | ||
|
|
||
| return { | ||
| suggestions: autosuggestProvider.provideResults(query).map(result => { | ||
| return { | ||
| label: result, | ||
| insertText: result, | ||
| overwriteBefore: alreadyTypedCount, | ||
| sortText: validatedAutoSuggestProvider.sortKey(result), | ||
| type: <modes.SuggestionType>'keyword' | ||
| }; | ||
| }) | ||
| }; | ||
| } | ||
| })); | ||
| } | ||
|
|
||
| public setValue(val: string) { | ||
| val = val.replace(/\s/g, ' '); | ||
| this.inputWidget.setValue(val); | ||
| this.inputWidget.setScrollTop(0); | ||
| this.inputWidget.setPosition(new Position(1, val.length + 1)); | ||
| } | ||
|
|
||
| public getValue(): string { | ||
| return this.inputWidget.getValue(); | ||
| } | ||
|
|
||
|
|
||
| public updateStyles(): void { | ||
| super.updateStyles(); | ||
|
|
||
| this.stylingContainer.style.backgroundColor = this.getColor(inputBackground); | ||
| this.stylingContainer.style.color = this.getColor(inputForeground); | ||
| this.placeholderText.style.color = this.getColor(inputPlaceholderForeground); | ||
|
|
||
| const inputBorderColor = this.getColor(inputBorder); | ||
| this.stylingContainer.style.borderWidth = inputBorderColor ? '1px' : null; | ||
| this.stylingContainer.style.borderStyle = inputBorderColor ? 'solid' : null; | ||
| this.stylingContainer.style.borderColor = inputBorderColor; | ||
|
|
||
| let cursor = this.stylingContainer.getElementsByClassName('cursor')[0] as HTMLDivElement; | ||
| if (cursor) { | ||
| cursor.style.backgroundColor = this.getColor(inputForeground); | ||
| } | ||
| } | ||
|
|
||
| public focus(): void { | ||
| this.inputWidget.focus(); | ||
| } | ||
|
|
||
| public layout(dimension: Dimension): void { | ||
| this.inputWidget.layout(dimension); | ||
| this.placeholderText.style.width = `${dimension.width}px`; | ||
| } | ||
|
|
||
| public selectAll(): void { | ||
| this.inputWidget.setSelection(new Range(1, 1, 1, this.getValue().length + 1)); | ||
| } | ||
|
|
||
| dispose(): void { | ||
| this.disposables = dispose(this.disposables); | ||
| super.dispose(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| function mixinHTMLInputStyleOptions(config: IEditorOptions, ariaLabel?: string): IEditorOptions { | ||
| config.fontSize = 13; | ||
| config.lineHeight = 22; | ||
| config.wordWrap = 'off'; | ||
| config.scrollbar.vertical = 'hidden'; | ||
| config.ariaLabel = ariaLabel || ''; | ||
| config.renderIndentGuides = false; | ||
| config.cursorWidth = 1; | ||
| config.snippetSuggestions = 'none'; | ||
| config.suggest = { filterGraceful: false }; | ||
| config.fontFamily = ' -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "HelveticaNeue-Light", "Ubuntu", "Droid Sans", sans-serif'; | ||
| return config; | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/vs/workbench/parts/codeEditor/browser/media/autosuggestEnabledInput.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| .autosuggest-input-container { | ||
| padding: 3px 4px 5px; | ||
| } | ||
|
|
||
| .autosuggest-input-container .suggest-widget { | ||
| width: 275px; | ||
| } | ||
|
|
||
| .autosuggest-input-container .monaco-editor-background, | ||
| .autosuggest-input-container .monaco-editor, | ||
| .autosuggest-input-container .mtk1 { | ||
| /* allow the embedded monaco to be styled from the outer context */ | ||
| background-color: inherit; | ||
| color: inherit; | ||
| } | ||
|
|
||
| .autosuggest-input-container .autosuggest-input-placeholder { | ||
| position: absolute; | ||
| z-index: 1; | ||
| overflow: hidden; | ||
| white-space: nowrap; | ||
| text-overflow: ellipsis; | ||
| pointer-events: none; | ||
| margin-top: 2px; | ||
| margin-left: 1px; | ||
| } | ||
File renamed without changes.
31 changes: 31 additions & 0 deletions
31
src/vs/workbench/parts/codeEditor/browser/simpleEditorOptions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { IEditorOptions } from 'vs/editor/common/config/editorOptions'; | ||
|
|
||
| export function getSimpleEditorOptions(): IEditorOptions { | ||
| return { | ||
| wordWrap: 'on', | ||
| overviewRulerLanes: 0, | ||
| glyphMargin: false, | ||
| lineNumbers: 'off', | ||
| folding: false, | ||
| selectOnLineNumbers: false, | ||
| hideCursorInOverviewRuler: true, | ||
| selectionHighlight: false, | ||
| scrollbar: { | ||
| horizontal: 'hidden' | ||
| }, | ||
| lineDecorationsWidth: 0, | ||
| overviewRulerBorder: false, | ||
| scrollBeyondLastLine: false, | ||
| renderLineHighlight: 'none', | ||
| fixedOverflowWidgets: true, | ||
| acceptSuggestionOnEnter: 'smart', | ||
| minimap: { | ||
| enabled: false | ||
| } | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove