提交 75ffa8be 编写于 作者: A Alex Dima

- Add a typed way to read options

- Move more options to the new format
上级 2a720880
......@@ -21,6 +21,7 @@ import { HorizontalRange } from 'vs/editor/common/view/renderingContext';
import { ViewContext } from 'vs/editor/common/view/viewContext';
import * as viewEvents from 'vs/editor/common/view/viewEvents';
import { ViewEventHandler } from 'vs/editor/common/viewModel/viewEventHandler';
import { EditorOptionId, EditorOption } from 'vs/editor/common/config/editorOptions';
/**
* Merges mouse events when mouse move events are throttled
......@@ -216,7 +217,7 @@ export class MouseHandler extends ViewEventHandler {
const targetIsContent = (t.type === editorBrowser.MouseTargetType.CONTENT_TEXT || t.type === editorBrowser.MouseTargetType.CONTENT_EMPTY);
const targetIsGutter = (t.type === editorBrowser.MouseTargetType.GUTTER_GLYPH_MARGIN || t.type === editorBrowser.MouseTargetType.GUTTER_LINE_NUMBERS || t.type === editorBrowser.MouseTargetType.GUTTER_LINE_DECORATIONS);
const targetIsLineNumbers = (t.type === editorBrowser.MouseTargetType.GUTTER_LINE_NUMBERS);
const selectOnLineNumbers = this._context.configuration.editor.viewInfo.selectOnLineNumbers;
const selectOnLineNumbers = this._context.configuration.getOption<typeof EditorOption.selectOnLineNumbers>(EditorOptionId.selectOnLineNumbers);
const targetIsViewZone = (t.type === editorBrowser.MouseTargetType.CONTENT_VIEW_ZONE || t.type === editorBrowser.MouseTargetType.GUTTER_VIEW_ZONE);
const targetIsWidget = (t.type === editorBrowser.MouseTargetType.CONTENT_WIDGET);
......
......@@ -536,6 +536,8 @@ export interface ICodeEditor extends editorCommon.IEditor {
*/
getConfiguration(): editorOptions.InternalEditorOptions;
getOption<T extends editorOptions.IEditorOption<any, any, any>>(id: editorOptions.EditorOptionId): editorOptions.ComputedEditorOptionValue<T>;
/**
* Returns the 'raw' editor's configuration (without any validation or defaults).
* @internal
......
......@@ -45,7 +45,7 @@ export class LineNumbersOverlay extends DynamicViewOverlay {
this._lineHeight = config.lineHeight;
this._renderLineNumbers = config.viewInfo.renderLineNumbers;
this._renderCustomLineNumbers = config.viewInfo.renderCustomLineNumbers;
this._renderFinalNewline = this._context.configuration.options.get(EditorOptionId.RenderFinalNewline, EditorOption.RenderFinalNewline);
this._renderFinalNewline = this._context.configuration.getOption<typeof EditorOption.renderFinalNewline>(EditorOptionId.renderFinalNewline);
this._lineNumbersLeft = config.layoutInfo.lineNumbersLeft;
this._lineNumbersWidth = config.layoutInfo.lineNumbersWidth;
}
......
......@@ -370,6 +370,10 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
return this._configuration.editor;
}
public getOption<T extends editorOptions.IEditorOption<any, any, any>>(id: editorOptions.EditorOptionId): editorOptions.ComputedEditorOptionValue<T> {
return this._configuration.getOption<T>(id);
}
public getRawConfiguration(): editorOptions.IEditorOptions {
return this._configuration.getRawOptions();
}
......
......@@ -156,6 +156,10 @@ export abstract class CommonEditorConfiguration extends Disposable implements ed
this._register(TabFocus.onDidChangeTabFocus(_ => this._recomputeOptions()));
}
public getOption<T extends editorOptions.IEditorOption<any, any, any>>(id: editorOptions.EditorOptionId): editorOptions.ComputedEditorOptionValue<T> {
return this.options._read(id);
}
public observeReferenceElement(dimension?: editorCommon.IDimension): void {
}
......@@ -345,7 +349,7 @@ const editorConfiguration: IConfigurationNode = {
},
'editor.renderFinalNewline': {
'type': 'boolean',
'default': editorOptions.EditorOption.RenderFinalNewline.defaultValue,
'default': editorOptions.EditorOption.renderFinalNewline.defaultValue,
'description': nls.localize('renderFinalNewline', "Render last line number when the file ends with a newline.")
},
'editor.rulers': {
......@@ -1137,7 +1141,7 @@ const editorConfiguration: IConfigurationNode = {
},
'editor.selectionClipboard': {
'type': 'boolean',
'default': EDITOR_DEFAULTS.contribInfo.selectionClipboard,
'default': editorOptions.EditorOption.selectionClipboard.defaultValue,
'description': nls.localize('selectionClipboard', "Controls whether the Linux primary clipboard should be supported."),
'included': platform.isLinux
},
......
......@@ -997,7 +997,6 @@ export interface InternalEditorViewOptions {
readonly renderLineNumbers: RenderLineNumbersType;
readonly renderCustomLineNumbers: ((lineNumber: number) => string) | null;
readonly cursorSurroundingLines: number;
readonly selectOnLineNumbers: boolean;
readonly glyphMargin: boolean;
readonly revealHorizontalRightPadding: number;
readonly roundedSelection: boolean;
......@@ -1025,7 +1024,6 @@ export interface InternalEditorViewOptions {
}
export interface EditorContribOptions {
readonly selectionClipboard: boolean;
readonly hover: InternalEditorHoverOptions;
readonly links: boolean;
readonly contextmenu: boolean;
......@@ -1317,7 +1315,6 @@ export class InternalEditorOptions {
&& a.renderLineNumbers === b.renderLineNumbers
&& a.renderCustomLineNumbers === b.renderCustomLineNumbers
&& a.cursorSurroundingLines === b.cursorSurroundingLines
&& a.selectOnLineNumbers === b.selectOnLineNumbers
&& a.glyphMargin === b.glyphMargin
&& a.revealHorizontalRightPadding === b.revealHorizontalRightPadding
&& a.roundedSelection === b.roundedSelection
......@@ -1464,8 +1461,7 @@ export class InternalEditorOptions {
*/
private static _equalsContribOptions(a: EditorContribOptions, b: EditorContribOptions): boolean {
return (
a.selectionClipboard === b.selectionClipboard
&& this._equalsHoverOptions(a.hover, b.hover)
this._equalsHoverOptions(a.hover, b.hover)
&& a.links === b.links
&& a.contextmenu === b.contextmenu
&& InternalEditorOptions._equalsQuickSuggestions(a.quickSuggestions, b.quickSuggestions)
......@@ -2081,7 +2077,6 @@ export class EditorOptionsValidator {
cursorSurroundingLines: _clampedInt(opts.cursorSurroundingLines, defaults.cursorWidth, 0, Number.MAX_VALUE),
renderLineNumbers: renderLineNumbers,
renderCustomLineNumbers: renderCustomLineNumbers,
selectOnLineNumbers: _boolean(opts.selectOnLineNumbers, defaults.selectOnLineNumbers),
glyphMargin: _boolean(opts.glyphMargin, defaults.glyphMargin),
revealHorizontalRightPadding: _clampedInt(opts.revealHorizontalRightPadding, defaults.revealHorizontalRightPadding, 0, 1000),
roundedSelection: _boolean(opts.roundedSelection, defaults.roundedSelection),
......@@ -2122,7 +2117,6 @@ export class EditorOptionsValidator {
}
const find = this._sanitizeFindOpts(opts.find, defaults.find);
return {
selectionClipboard: _boolean(opts.selectionClipboard, defaults.selectionClipboard),
hover: this._sanitizeHoverOpts(opts.hover, defaults.hover),
links: _boolean(opts.links, defaults.links),
contextmenu: _boolean(opts.contextmenu, defaults.contextmenu),
......@@ -2202,7 +2196,6 @@ export class InternalEditorOptionsFactory {
renderLineNumbers: opts.viewInfo.renderLineNumbers,
renderCustomLineNumbers: opts.viewInfo.renderCustomLineNumbers,
cursorSurroundingLines: opts.viewInfo.cursorSurroundingLines,
selectOnLineNumbers: opts.viewInfo.selectOnLineNumbers,
glyphMargin: opts.viewInfo.glyphMargin,
revealHorizontalRightPadding: opts.viewInfo.revealHorizontalRightPadding,
roundedSelection: opts.viewInfo.roundedSelection,
......@@ -2236,7 +2229,6 @@ export class InternalEditorOptionsFactory {
},
contribInfo: {
selectionClipboard: opts.contribInfo.selectionClipboard,
hover: opts.contribInfo.hover,
links: opts.contribInfo.links,
contextmenu: opts.contribInfo.contextmenu,
......@@ -2668,7 +2660,6 @@ export const EDITOR_DEFAULTS: IValidatedEditorOptions = {
renderLineNumbers: RenderLineNumbersType.On,
renderCustomLineNumbers: null,
cursorSurroundingLines: 0,
selectOnLineNumbers: true,
glyphMargin: true,
revealHorizontalRightPadding: 30,
roundedSelection: true,
......@@ -2716,7 +2707,6 @@ export const EDITOR_DEFAULTS: IValidatedEditorOptions = {
},
contribInfo: {
selectionClipboard: true,
hover: {
enabled: true,
delay: 300,
......@@ -2803,10 +2793,6 @@ export class ValidatedEditorOptions {
}
}
export interface IComputedEditorOptions {
get<T1, T2, T3>(id: EditorOptionId, option: IEditorOption<T1, T2, T3>): T3;
}
/**
* @internal
*/
......@@ -2815,9 +2801,6 @@ export class ComputedEditorOptions {
public _read<T>(id: EditorOptionId): T {
return this._values[id];
}
public get<T1, T2, T3>(id: EditorOptionId, option: IEditorOption<T1, T2, T3>): T3 {
return this._values[id];
}
public _write<T>(id: EditorOptionId, value: T): void {
this._values[id] = value;
}
......@@ -2836,7 +2819,7 @@ export class ChangedEditorOptions {
}
}
interface IEditorOption<T1, T2 = T1, T3 = T2> {
export interface IEditorOption<T1, T2 = T1, T3 = T2> {
readonly id: EditorOptionId;
readonly name: string;
readonly defaultValue: T1;
......@@ -2890,9 +2873,15 @@ function registerEditorOption<T1, T2, T3>(option: IEditorOption<T1, T2, T3>): IE
}
export const enum EditorOptionId {
RenderFinalNewline,
renderFinalNewline,
selectionClipboard,
selectOnLineNumbers,
}
export const EditorOption = {
RenderFinalNewline: registerEditorOption(new BooleanEditorOption(EditorOptionId.RenderFinalNewline, 'renderFinalNewline', true))
renderFinalNewline: registerEditorOption(new BooleanEditorOption(EditorOptionId.renderFinalNewline, 'renderFinalNewline', true)),
selectionClipboard: registerEditorOption(new BooleanEditorOption(EditorOptionId.selectionClipboard, 'selectionClipboard', true)),
selectOnLineNumbers: registerEditorOption(new BooleanEditorOption(EditorOptionId.selectOnLineNumbers, 'selectOnLineNumbers', true)),
};
export type ComputedEditorOptionValue<T extends IEditorOption<any, any, any>> = T extends IEditorOption<any, any, infer R> ? R : never;
......@@ -152,7 +152,8 @@ export interface IConfiguration extends IDisposable {
onDidChange(listener: (e: editorOptions.IConfigurationChangedEvent) => void): IDisposable;
readonly editor: editorOptions.InternalEditorOptions;
readonly options: editorOptions.IComputedEditorOptions;
getOption<T extends editorOptions.IEditorOption<any, any, any>>(id: editorOptions.EditorOptionId): editorOptions.ComputedEditorOptionValue<T>;
setMaxLineNumber(maxLineNumber: number): void;
updateOptions(newOptions: editorOptions.IEditorOptions): void;
......
......@@ -431,7 +431,9 @@ export enum RenderLineNumbersType {
}
export enum EditorOptionId {
RenderFinalNewline = 0
renderFinalNewline = 0,
selectionClipboard = 1,
selectOnLineNumbers = 2
}
/**
......
......@@ -3312,7 +3312,6 @@ declare namespace monaco.editor {
readonly renderLineNumbers: RenderLineNumbersType;
readonly renderCustomLineNumbers: ((lineNumber: number) => string) | null;
readonly cursorSurroundingLines: number;
readonly selectOnLineNumbers: boolean;
readonly glyphMargin: boolean;
readonly revealHorizontalRightPadding: number;
readonly roundedSelection: boolean;
......@@ -3340,7 +3339,6 @@ declare namespace monaco.editor {
}
export interface EditorContribOptions {
readonly selectionClipboard: boolean;
readonly hover: InternalEditorHoverOptions;
readonly links: boolean;
readonly contextmenu: boolean;
......@@ -3555,11 +3553,7 @@ declare namespace monaco.editor {
[key: string]: any;
}
export interface IComputedEditorOptions {
get<T1, T2, T3>(id: EditorOptionId, option: IEditorOption<T1, T2, T3>): T3;
}
interface IEditorOption<T1, T2 = T1, T3 = T2> {
export interface IEditorOption<T1, T2 = T1, T3 = T2> {
readonly id: EditorOptionId;
readonly name: string;
readonly defaultValue: T1;
......@@ -3571,13 +3565,19 @@ declare namespace monaco.editor {
}
export enum EditorOptionId {
RenderFinalNewline = 0
renderFinalNewline = 0,
selectionClipboard = 1,
selectOnLineNumbers = 2
}
export const EditorOption: {
RenderFinalNewline: IEditorOption<boolean, boolean, boolean>;
renderFinalNewline: IEditorOption<boolean, boolean, boolean>;
selectionClipboard: IEditorOption<boolean, boolean, boolean>;
selectOnLineNumbers: IEditorOption<boolean, boolean, boolean>;
};
export type ComputedEditorOptionValue<T extends IEditorOption<any, any, any>> = T extends IEditorOption<any, any, infer R> ? R : never;
/**
* A view zone is a full horizontal rectangle that 'pushes' text down.
* The editor reserves space for view zones when rendering.
......@@ -4027,6 +4027,7 @@ declare namespace monaco.editor {
* Returns the current editor's configuration
*/
getConfiguration(): InternalEditorOptions;
getOption<T extends IEditorOption<any, any, any>>(id: EditorOptionId): ComputedEditorOptionValue<T>;
/**
* Get value of the current model attached to this editor.
* @see `ITextModel.getValue`
......
......@@ -9,7 +9,7 @@ import * as process from 'vs/base/common/process';
import * as platform from 'vs/base/common/platform';
import { ICodeEditor, IEditorMouseEvent, MouseTargetType } from 'vs/editor/browser/editorBrowser';
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions';
import { IConfigurationChangedEvent } from 'vs/editor/common/config/editorOptions';
import { IConfigurationChangedEvent, EditorOption, EditorOptionId } from 'vs/editor/common/config/editorOptions';
import { ICursorSelectionChangedEvent } from 'vs/editor/common/controller/cursorEvents';
import { Range } from 'vs/editor/common/core/range';
import { IEditorContribution } from 'vs/editor/common/editorCommon';
......@@ -24,11 +24,11 @@ export class SelectionClipboard extends Disposable implements IEditorContributio
super();
if (platform.isLinux) {
let isEnabled = editor.getConfiguration().contribInfo.selectionClipboard;
let isEnabled = editor.getOption<typeof EditorOption.selectionClipboard>(EditorOptionId.selectionClipboard);
this._register(editor.onDidChangeConfiguration((e: IConfigurationChangedEvent) => {
if (e.contribInfo) {
isEnabled = editor.getConfiguration().contribInfo.selectionClipboard;
if (e.hasChanged(EditorOptionId.selectionClipboard)) {
isEnabled = editor.getOption<typeof EditorOption.selectionClipboard>(EditorOptionId.selectionClipboard);
}
}));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册