toggleWordWrap.ts 11.2 KB
Newer Older
I
ivanixgames 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

A
Alex Dima 已提交
6
import * as nls from 'vs/nls';
J
Johannes Rieken 已提交
7
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
A
Alex Dima 已提交
8
import { Disposable } from 'vs/base/common/lifecycle';
9
import { URI } from 'vs/base/common/uri';
10
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
A
Alex Dima 已提交
11 12
import { EditorAction, ServicesAccessor, registerEditorAction, registerEditorContribution } from 'vs/editor/browser/editorExtensions';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
13
import { EditorOptionId, EditorOption } from 'vs/editor/common/config/editorOptions';
A
Alex Dima 已提交
14 15 16 17 18
import { IEditorContribution } from 'vs/editor/common/editorCommon';
import { ITextModel } from 'vs/editor/common/model';
import { ITextResourceConfigurationService } from 'vs/editor/common/services/resourceConfiguration';
import { MenuId, MenuRegistry } from 'vs/platform/actions/common/actions';
import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
19
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
A
Alex Dima 已提交
20
import { INotificationService } from 'vs/platform/notification/common/notification';
21
import { DefaultSettingsEditorContribution } from 'vs/workbench/contrib/preferences/browser/preferencesEditor';
A
Alex Dima 已提交
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36
const transientWordWrapState = 'transientWordWrapState';
const isWordWrapMinifiedKey = 'isWordWrapMinified';
const isDominatedByLongLinesKey = 'isDominatedByLongLines';
const inDiffEditorKey = 'inDiffEditor';

/**
 * State written/read by the toggle word wrap action and associated with a particular model.
 */
interface IWordWrapTransientState {
	readonly forceWordWrap: 'on' | 'off' | 'wordWrapColumn' | 'bounded';
	readonly forceWordWrapMinified: boolean;
}

interface IWordWrapState {
A
Alex Dima 已提交
37
	readonly configuredWordWrap: 'on' | 'off' | 'wordWrapColumn' | 'bounded' | undefined;
38
	readonly configuredWordWrapMinified: boolean;
A
Alex Dima 已提交
39
	readonly transientState: IWordWrapTransientState | null;
40 41 42 43 44
}

/**
 * Store (in memory) the word wrap state for a particular model.
 */
45
export function writeTransientState(model: ITextModel, state: IWordWrapTransientState | null, codeEditorService: ICodeEditorService): void {
46 47 48 49 50 51
	codeEditorService.setTransientModelProperty(model, transientWordWrapState, state);
}

/**
 * Read (in memory) the word wrap state for a particular model.
 */
A
Alex Dima 已提交
52
function readTransientState(model: ITextModel, codeEditorService: ICodeEditorService): IWordWrapTransientState {
53 54 55
	return codeEditorService.getTransientModelProperty(model, transientWordWrapState);
}

A
Alex Dima 已提交
56
function readWordWrapState(model: ITextModel, configurationService: ITextResourceConfigurationService, codeEditorService: ICodeEditorService): IWordWrapState {
57
	const editorConfig = configurationService.getValue(model.uri, 'editor') as { wordWrap: 'on' | 'off' | 'wordWrapColumn' | 'bounded'; wordWrapMinified: boolean };
R
Rob Lourens 已提交
58
	let _configuredWordWrap = editorConfig && (typeof editorConfig.wordWrap === 'string' || typeof editorConfig.wordWrap === 'boolean') ? editorConfig.wordWrap : undefined;
A
Alex Dima 已提交
59 60 61 62 63 64 65 66

	// Compatibility with old true or false values
	if (<any>_configuredWordWrap === true) {
		_configuredWordWrap = 'on';
	} else if (<any>_configuredWordWrap === false) {
		_configuredWordWrap = 'off';
	}

R
Rob Lourens 已提交
67
	const _configuredWordWrapMinified = editorConfig && typeof editorConfig.wordWrapMinified === 'boolean' ? editorConfig.wordWrapMinified : undefined;
68 69
	const _transientState = readTransientState(model, codeEditorService);
	return {
A
Alex Dima 已提交
70
		configuredWordWrap: _configuredWordWrap,
A
Alex Dima 已提交
71
		configuredWordWrapMinified: (typeof _configuredWordWrapMinified === 'boolean' ? _configuredWordWrapMinified : EditorOption.wordWrapMinified.defaultValue),
72 73 74 75
		transientState: _transientState
	};
}

76
function toggleWordWrap(editor: ICodeEditor, state: IWordWrapState): IWordWrapState {
77 78 79 80 81 82 83 84 85 86 87
	if (state.transientState) {
		// toggle off => go to null
		return {
			configuredWordWrap: state.configuredWordWrap,
			configuredWordWrapMinified: state.configuredWordWrapMinified,
			transientState: null
		};
	}

	let transientState: IWordWrapTransientState;

88
	const actualWrappingInfo = editor.getOption(EditorOptionId.wrappingInfo);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	if (actualWrappingInfo.isWordWrapMinified) {
		// => wrapping due to minified file
		transientState = {
			forceWordWrap: 'off',
			forceWordWrapMinified: false
		};
	} else if (state.configuredWordWrap !== 'off') {
		// => wrapping is configured to be on (or some variant)
		transientState = {
			forceWordWrap: 'off',
			forceWordWrapMinified: false
		};
	} else {
		// => wrapping is configured to be off
		transientState = {
			forceWordWrap: 'on',
			forceWordWrapMinified: state.configuredWordWrapMinified
		};
	}

	return {
		configuredWordWrap: state.configuredWordWrap,
		configuredWordWrapMinified: state.configuredWordWrapMinified,
		transientState: transientState
	};
}

116
const TOGGLE_WORD_WRAP_ID = 'editor.action.toggleWordWrap';
A
Alex Dima 已提交
117
class ToggleWordWrapAction extends EditorAction {
A
Alex Dima 已提交
118 119

	constructor() {
120
		super({
121
			id: TOGGLE_WORD_WRAP_ID,
122 123
			label: nls.localize('toggle.wordwrap', "View: Toggle Word Wrap"),
			alias: 'View: Toggle Word Wrap',
124
			precondition: undefined,
125
			kbOpts: {
126
				kbExpr: null,
A
Alex Dima 已提交
127
				primary: KeyMod.Alt | KeyCode.KEY_Z,
128
				weight: KeybindingWeight.EditorContrib
129 130
			}
		});
I
ivanixgames 已提交
131 132
	}

133
	public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
134 135 136 137
		if (editor.getContribution(DefaultSettingsEditorContribution.ID)) {
			// in the settings editor...
			return;
		}
A
Alex Dima 已提交
138 139 140
		if (!editor.hasModel()) {
			return;
		}
141
		if (editor.getOption(EditorOptionId.inDiffEditor)) {
A
Alex Dima 已提交
142
			// Cannot change wrapping settings inside the diff editor
143 144
			const notificationService = accessor.get(INotificationService);
			notificationService.info(nls.localize('wordWrap.notInDiffEditor', "Cannot toggle word wrap in a diff editor."));
A
Alex Dima 已提交
145 146 147
			return;
		}

148
		const textResourceConfigurationService = accessor.get(ITextResourceConfigurationService);
149
		const codeEditorService = accessor.get(ICodeEditorService);
150 151
		const model = editor.getModel();

152 153 154 155
		if (!canToggleWordWrap(model.uri)) {
			return;
		}

156
		// Read the current state
157
		const currentState = readWordWrapState(model, textResourceConfigurationService, codeEditorService);
158 159 160
		// Compute the new state
		const newState = toggleWordWrap(editor, currentState);
		// Write the new state
A
Alex Dima 已提交
161
		// (this will cause an event and the controller will apply the state)
162
		writeTransientState(model, newState.transientState, codeEditorService);
I
ivanixgames 已提交
163 164
	}
}
165 166 167

class ToggleWordWrapController extends Disposable implements IEditorContribution {

168
	private static readonly _ID = 'editor.contrib.toggleWordWrapController';
169 170

	constructor(
171
		private readonly editor: ICodeEditor,
172
		@IContextKeyService readonly contextKeyService: IContextKeyService,
173
		@ITextResourceConfigurationService readonly configurationService: ITextResourceConfigurationService,
174
		@ICodeEditorService readonly codeEditorService: ICodeEditorService
175 176 177
	) {
		super();

A
Alex Dima 已提交
178
		const options = this.editor.getOptions();
179
		const wrappingInfo = options.get(EditorOptionId.wrappingInfo);
A
Alex Dima 已提交
180 181
		const isWordWrapMinified = this.contextKeyService.createKey(isWordWrapMinifiedKey, wrappingInfo.isWordWrapMinified);
		const isDominatedByLongLines = this.contextKeyService.createKey(isDominatedByLongLinesKey, wrappingInfo.isDominatedByLongLines);
182
		const inDiffEditor = this.contextKeyService.createKey(inDiffEditorKey, options.get(EditorOptionId.inDiffEditor));
A
Alex Dima 已提交
183
		let currentlyApplyingEditorConfig = false;
184 185

		this._register(editor.onDidChangeConfiguration((e) => {
A
Alex Dima 已提交
186
			if (!e.hasChanged(EditorOptionId.wrappingInfo) || !e.hasChanged(EditorOptionId.inDiffEditor)) {
187 188
				return;
			}
A
Alex Dima 已提交
189
			const options = this.editor.getOptions();
190
			const wrappingInfo = options.get(EditorOptionId.wrappingInfo);
A
Alex Dima 已提交
191 192
			isWordWrapMinified.set(wrappingInfo.isWordWrapMinified);
			isDominatedByLongLines.set(wrappingInfo.isDominatedByLongLines);
193
			inDiffEditor.set(options.get(EditorOptionId.inDiffEditor));
A
Alex Dima 已提交
194 195 196 197
			if (!currentlyApplyingEditorConfig) {
				// I am not the cause of the word wrap getting changed
				ensureWordWrapSettings();
			}
198
		}));
199 200

		this._register(editor.onDidChangeModel((e) => {
A
Alex Dima 已提交
201 202 203
			ensureWordWrapSettings();
		}));

A
Alex Dima 已提交
204 205 206 207
		this._register(codeEditorService.onDidChangeTransientModelProperty(() => {
			ensureWordWrapSettings();
		}));

A
Alex Dima 已提交
208
		const ensureWordWrapSettings = () => {
209 210 211 212
			if (this.editor.getContribution(DefaultSettingsEditorContribution.ID)) {
				// in the settings editor...
				return;
			}
213 214 215 216 217 218
			// Ensure correct word wrap settings
			const newModel = this.editor.getModel();
			if (!newModel) {
				return;
			}

219
			if (this.editor.getOption(EditorOptionId.inDiffEditor)) {
220 221 222
				return;
			}

223 224 225 226
			if (!canToggleWordWrap(newModel.uri)) {
				return;
			}

227 228 229 230
			// Read current configured values and toggle state
			const desiredState = readWordWrapState(newModel, this.configurationService, this.codeEditorService);

			// Apply the state
A
Alex Dima 已提交
231 232
			try {
				currentlyApplyingEditorConfig = true;
A
Alex Dima 已提交
233
				this._applyWordWrapState(desiredState);
A
Alex Dima 已提交
234 235 236 237
			} finally {
				currentlyApplyingEditorConfig = false;
			}
		};
238 239
	}

A
Alex Dima 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	private _applyWordWrapState(state: IWordWrapState): void {
		if (state.transientState) {
			// toggle is on
			this.editor.updateOptions({
				wordWrap: state.transientState.forceWordWrap,
				wordWrapMinified: state.transientState.forceWordWrapMinified
			});
			return;
		}

		// toggle is off
		this.editor.updateOptions({
			wordWrap: state.configuredWordWrap,
			wordWrapMinified: state.configuredWordWrapMinified
		});
	}

257 258 259 260 261
	public getId(): string {
		return ToggleWordWrapController._ID;
	}
}

262 263 264 265
function canToggleWordWrap(uri: URI): boolean {
	if (!uri) {
		return false;
	}
266
	return (uri.scheme !== 'output');
267 268
}

269

270
registerEditorContribution(ToggleWordWrapController);
271

272
registerEditorAction(ToggleWordWrapAction);
273

274 275
MenuRegistry.appendMenuItem(MenuId.EditorTitle, {
	command: {
276
		id: TOGGLE_WORD_WRAP_ID,
A
Alex Dima 已提交
277
		title: nls.localize('unwrapMinified', "Disable wrapping for this file"),
278 279 280 281
		iconLocation: {
			dark: URI.parse(require.toUrl('vs/workbench/contrib/codeEditor/browser/word-wrap-dark.svg')),
			light: URI.parse(require.toUrl('vs/workbench/contrib/codeEditor/browser/word-wrap-light.svg'))
		}
A
Alex Dima 已提交
282 283 284 285
	},
	group: 'navigation',
	order: 1,
	when: ContextKeyExpr.and(
286 287 288
		ContextKeyExpr.not(inDiffEditorKey),
		ContextKeyExpr.has(isDominatedByLongLinesKey),
		ContextKeyExpr.has(isWordWrapMinifiedKey)
A
Alex Dima 已提交
289 290 291 292
	)
});
MenuRegistry.appendMenuItem(MenuId.EditorTitle, {
	command: {
293
		id: TOGGLE_WORD_WRAP_ID,
A
Alex Dima 已提交
294
		title: nls.localize('wrapMinified', "Enable wrapping for this file"),
295 296 297 298
		iconLocation: {
			dark: URI.parse(require.toUrl('vs/workbench/contrib/codeEditor/browser/word-wrap-dark.svg')),
			light: URI.parse(require.toUrl('vs/workbench/contrib/codeEditor/browser/word-wrap-light.svg'))
		}
299 300 301
	},
	group: 'navigation',
	order: 1,
A
Alex Dima 已提交
302
	when: ContextKeyExpr.and(
303 304 305
		ContextKeyExpr.not(inDiffEditorKey),
		ContextKeyExpr.has(isDominatedByLongLinesKey),
		ContextKeyExpr.not(isWordWrapMinifiedKey)
A
Alex Dima 已提交
306
	)
307
});
308 309 310 311 312 313 314 315 316 317 318


// View menu
MenuRegistry.appendMenuItem(MenuId.MenubarViewMenu, {
	group: '5_editor',
	command: {
		id: TOGGLE_WORD_WRAP_ID,
		title: nls.localize({ key: 'miToggleWordWrap', comment: ['&& denotes a mnemonic'] }, "Toggle &&Word Wrap")
	},
	order: 1
});