Skip to content

Commit

Permalink
implement settings proposal, #9504
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Mar 9, 2017
1 parent 6d2013a commit 95096bd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 35 deletions.
21 changes: 20 additions & 1 deletion src/vs/editor/common/config/commonEditorConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,26 @@ const editorConfiguration: IConfigurationNode = {
'description': nls.localize('emptySelectionClipboard', "Controls whether copying without a selection copies the current line.")
},
'editor.wordBasedSuggestions': {
'type': 'boolean',
'anyOf': [
'boolean',
{
type: 'object',
properties: {
strings: {
type: 'boolean',
description: nls.localize('wordBasedSuggestions.strings', "Enable word based suggestions inside strings.")
},
comments: {
type: 'boolean',
description: nls.localize('wordBasedSuggestions.comments', "Enable word based suggestions inside comments.")
},
default: {
type: 'boolean',
description: nls.localize('wordBasedSuggestions.default', "Enable word based suggestions outside of strings and comments.")
},
}
}
],
'default': DefaultConfig.editor.wordBasedSuggestions,
'description': nls.localize('wordBasedSuggestions', "Enable word based suggestions.")
},
Expand Down
9 changes: 5 additions & 4 deletions src/vs/editor/common/editorCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { BulkListenerCallback } from 'vs/base/common/eventEmitter';
import { MarkedString } from 'vs/base/common/htmlContent';
import * as types from 'vs/base/common/types';
import * as objects from 'vs/base/common/objects';
import URI from 'vs/base/common/uri';
import { TPromise } from 'vs/base/common/winjs.base';
import { ServicesAccessor, IConstructorSignature1 } from 'vs/platform/instantiation/common/instantiation';
Expand Down Expand Up @@ -481,7 +482,7 @@ export interface IEditorOptions {
/**
* Enable word based suggestions. Defaults to 'true'
*/
wordBasedSuggestions?: boolean;
wordBasedSuggestions?: boolean | { strings?: boolean, comments?: boolean, default?: boolean };
/**
* The font size for the suggest widget.
* Defaults to the editor font size.
Expand Down Expand Up @@ -1015,7 +1016,7 @@ export class EditorContribOptions {
readonly acceptSuggestionOnCommitCharacter: boolean;
readonly snippetSuggestions: 'top' | 'bottom' | 'inline' | 'none';
readonly emptySelectionClipboard: boolean;
readonly wordBasedSuggestions: boolean;
readonly wordBasedSuggestions: boolean | { strings?: boolean, comments?: boolean, default?: boolean };
readonly suggestFontSize: number;
readonly suggestLineHeight: number;
readonly selectionHighlight: boolean;
Expand All @@ -1042,7 +1043,7 @@ export class EditorContribOptions {
acceptSuggestionOnCommitCharacter: boolean;
snippetSuggestions: 'top' | 'bottom' | 'inline' | 'none';
emptySelectionClipboard: boolean;
wordBasedSuggestions: boolean;
wordBasedSuggestions: boolean | { strings?: boolean, comments?: boolean, default?: boolean };
suggestFontSize: number;
suggestLineHeight: number;
selectionHighlight: boolean;
Expand Down Expand Up @@ -1094,7 +1095,7 @@ export class EditorContribOptions {
&& this.acceptSuggestionOnCommitCharacter === other.acceptSuggestionOnCommitCharacter
&& this.snippetSuggestions === other.snippetSuggestions
&& this.emptySelectionClipboard === other.emptySelectionClipboard
&& this.wordBasedSuggestions === other.wordBasedSuggestions
&& objects.equals(this.wordBasedSuggestions, other.wordBasedSuggestions)
&& this.suggestFontSize === other.suggestFontSize
&& this.suggestLineHeight === other.suggestLineHeight
&& this.selectionHighlight === other.selectionHighlight
Expand Down
48 changes: 20 additions & 28 deletions src/vs/editor/common/services/editorWorkerServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,6 @@ export class EditorWorkerServiceImpl implements IEditorWorkerService {
}
}

class WordBasedCompletionConfig {

static fromOldConfig(value: boolean): WordBasedCompletionConfig {
return new WordBasedCompletionConfig(value, value, value);
}

constructor(
readonly suggestInStrings: boolean,
readonly suggestInComments: boolean,
readonly suggestInCode: boolean
) {

}

accept(tokenType: modes.StandardTokenType): boolean {
return (tokenType === modes.StandardTokenType.String && this.suggestInStrings)
|| (tokenType === modes.StandardTokenType.Comment && this.suggestInComments)
|| (tokenType === modes.StandardTokenType.Other && this.suggestInCode);
}
}

class WordBasedCompletionItemProvider implements modes.ISuggestSupport {

private readonly _workerManager: WorkerManager;
Expand All @@ -110,16 +89,29 @@ class WordBasedCompletionItemProvider implements modes.ISuggestSupport {

provideCompletionItems(model: editorCommon.IModel, position: Position): TPromise<modes.ISuggestResult> {

// TODO(joh) should we force tokenization?
// model.forceTokenization(position.lineNumber);
const config = WordBasedCompletionConfig.fromOldConfig(this._configurationService.lookup<boolean>('editor.wordBasedSuggestions').value);
const tokens = model.getLineTokens(position.lineNumber);
const { tokenType } = tokens.findTokenAtOffset(position.column - 1);
const { wordBasedSuggestions } = this._configurationService.getConfiguration<editorCommon.IEditorOptions>('editor');

if (wordBasedSuggestions === false) {
// simple -> disabled everywhere
return undefined;

if (config.accept(tokenType)) {
} else if (wordBasedSuggestions === true) {
// simple -> enabled for all tokens
return this._workerManager.withWorker().then(client => client.textualSuggest(model.uri, position));

} else {
return undefined;
// check with token type and config
const tokens = model.getLineTokens(position.lineNumber);
const { tokenType } = tokens.findTokenAtOffset(position.column - 1);
const shoudSuggestHere = (tokenType === modes.StandardTokenType.Comment && wordBasedSuggestions.comments)
|| (tokenType === modes.StandardTokenType.String && wordBasedSuggestions.strings)
|| (tokenType === modes.StandardTokenType.Other && wordBasedSuggestions.default);

if (shoudSuggestHere) {
return this._workerManager.withWorker().then(client => client.textualSuggest(model.uri, position));
} else {
return undefined;
}
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,11 @@ declare module monaco.editor {
/**
* Enable word based suggestions. Defaults to 'true'
*/
wordBasedSuggestions?: boolean;
wordBasedSuggestions?: boolean | {
strings?: boolean;
comments?: boolean;
default?: boolean;
};
/**
* The font size for the suggest widget.
* Defaults to the editor font size.
Expand Down Expand Up @@ -1619,7 +1623,11 @@ declare module monaco.editor {
readonly acceptSuggestionOnCommitCharacter: boolean;
readonly snippetSuggestions: 'top' | 'bottom' | 'inline' | 'none';
readonly emptySelectionClipboard: boolean;
readonly wordBasedSuggestions: boolean;
readonly wordBasedSuggestions: boolean | {
strings?: boolean;
comments?: boolean;
default?: boolean;
};
readonly suggestFontSize: number;
readonly suggestLineHeight: number;
readonly selectionHighlight: boolean;
Expand Down

0 comments on commit 95096bd

Please sign in to comment.