提交 74245ac1 编写于 作者: A Alex Dima

Fixes #21656: Toggle Wordwrap should toggle back to configured value

上级 60f1c78b
......@@ -5,7 +5,7 @@
'use strict';
import Event, { Emitter } from 'vs/base/common/event';
import { ICommonCodeEditor, ICommonDiffEditor, IDecorationRenderOptions, IModelDecorationOptions } from 'vs/editor/common/editorCommon';
import { ICommonCodeEditor, ICommonDiffEditor, IDecorationRenderOptions, IModelDecorationOptions, IModel } from 'vs/editor/common/editorCommon';
import { ICodeEditorService } from 'vs/editor/common/services/codeEditorService';
export abstract class AbstractCodeEditorService implements ICodeEditorService {
......@@ -106,4 +106,53 @@ export abstract class AbstractCodeEditorService implements ICodeEditorService {
abstract registerDecorationType(key: string, options: IDecorationRenderOptions, parentTypeKey?: string): void;
abstract removeDecorationType(key: string): void;
abstract resolveDecorationOptions(decorationTypeKey: string, writable: boolean): IModelDecorationOptions;
private _transientWatchers: { [uri: string]: ModelTransientSettingWatcher; } = {};
public setTransientModelProperty(model: IModel, key: string, value: any): void {
const uri = model.uri.toString();
let w: ModelTransientSettingWatcher;
if (this._transientWatchers.hasOwnProperty(uri)) {
w = this._transientWatchers[uri];
} else {
w = new ModelTransientSettingWatcher(uri, model, this);
this._transientWatchers[uri] = w;
}
w.set(key, value);
}
public getTransientModelProperty(model: IModel, key: string): any {
const uri = model.uri.toString();
if (!this._transientWatchers.hasOwnProperty(uri)) {
return undefined;
}
return this._transientWatchers[uri].get(key);
}
_removeWatcher(w: ModelTransientSettingWatcher): void {
delete this._transientWatchers[w.uri];
}
}
export class ModelTransientSettingWatcher {
public readonly uri: string;
private readonly _values: { [key: string]: any; };
constructor(uri: string, model: IModel, owner: AbstractCodeEditorService) {
this.uri = uri;
this._values = {};
model.onWillDispose(() => owner._removeWatcher(this));
}
public set(key: string, value: any): void {
this._values[key] = value;
}
public get(key: string): any {
return this._values[key];
}
}
......@@ -6,7 +6,7 @@
import Event from 'vs/base/common/event';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { ICommonCodeEditor, ICommonDiffEditor, isCommonCodeEditor, isCommonDiffEditor, IDecorationRenderOptions, IModelDecorationOptions } from 'vs/editor/common/editorCommon';
import { ICommonCodeEditor, ICommonDiffEditor, isCommonCodeEditor, isCommonDiffEditor, IDecorationRenderOptions, IModelDecorationOptions, IModel } from 'vs/editor/common/editorCommon';
import { IEditor } from 'vs/platform/editor/common/editor';
export var ICodeEditorService = createDecorator<ICodeEditorService>('codeEditorService');
......@@ -38,6 +38,9 @@ export interface ICodeEditorService {
registerDecorationType(key: string, options: IDecorationRenderOptions, parentTypeKey?: string): void;
removeDecorationType(key: string): void;
resolveDecorationOptions(typeKey: string, writable: boolean): IModelDecorationOptions;
setTransientModelProperty(model: IModel, key: string, value: any): void;
getTransientModelProperty(model: IModel, key: string): any;
}
/**
......
......@@ -8,6 +8,9 @@ import * as nls from 'vs/nls';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { ICommonCodeEditor } from 'vs/editor/common/editorCommon';
import { editorAction, ServicesAccessor, EditorAction } from 'vs/editor/common/editorCommonExtensions';
import { ICodeEditorService } from 'vs/editor/common/services/codeEditorService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { DefaultConfig } from 'vs/editor/common/config/defaultConfig';
@editorAction
class ToggleWordWrapAction extends EditorAction {
......@@ -26,20 +29,35 @@ class ToggleWordWrapAction extends EditorAction {
}
public run(accessor: ServicesAccessor, editor: ICommonCodeEditor): void {
let wrappingInfo = editor.getConfiguration().wrappingInfo;
let newWordWrap: 'on' | 'off';
let newWordWrapMinified: boolean;
if (!wrappingInfo.isViewportWrapping) {
newWordWrap = 'on';
newWordWrapMinified = true;
const codeEditorService = accessor.get(ICodeEditorService);
const configurationService = accessor.get(IConfigurationService);
const model = editor.getModel();
const _configuredWordWrap = configurationService.lookup<'on' | 'off' | 'wordWrapColumn' | 'bounded'>('editor.wordWrap', model.getLanguageIdentifier().language);
const _configuredWordWrapMinified = configurationService.lookup<boolean>('editor.wordWrapMinified', model.getLanguageIdentifier().language);
const configuredWordWrap = _configuredWordWrap.value;
const configuredWordWrapMinified = (typeof _configuredWordWrapMinified.value === 'undefined' ? DefaultConfig.editor.wordWrapMinified : _configuredWordWrapMinified.value);
const alreadyToggled = codeEditorService.getTransientModelProperty(model, 'toggleWordWrap');
if (!alreadyToggled) {
codeEditorService.setTransientModelProperty(model, 'toggleWordWrap', true);
if (configuredWordWrap !== 'off') {
editor.updateOptions({
wordWrap: 'off',
wordWrapMinified: false
});
} else {
editor.updateOptions({
wordWrap: 'on'
});
}
} else {
newWordWrap = 'off';
newWordWrapMinified = false;
codeEditorService.setTransientModelProperty(model, 'toggleWordWrap', false);
editor.updateOptions({
wordWrap: configuredWordWrap,
wordWrapMinified: configuredWordWrapMinified
});
}
editor.updateOptions({
wordWrap: newWordWrap,
wordWrapMinified: newWordWrapMinified
});
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册