|
3 | 3 | * Licensed under the MIT License. See License.txt in the project root for license information.
|
4 | 4 | *--------------------------------------------------------------------------------------------*/
|
5 | 5 |
|
| 6 | +import * as assert from 'assert'; |
6 | 7 | import { Selection } from 'vs/editor/common/core/selection';
|
7 |
| -import { IndentationToSpacesCommand, IndentationToTabsCommand } from 'vs/editor/contrib/indentation/browser/indentation'; |
| 8 | +import { Range } from 'vs/editor/common/core/range'; |
| 9 | +import { AutoIndentOnPaste, IndentationToSpacesCommand, IndentationToTabsCommand } from 'vs/editor/contrib/indentation/browser/indentation'; |
8 | 10 | import { testCommand } from 'vs/editor/test/browser/testCommand';
|
| 11 | +import { withTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor'; |
| 12 | +import { javascriptIndentationRules } from 'vs/editor/test/common/modes/supports/javascriptIndentationRules'; |
| 13 | +import { javascriptOnEnterRules } from 'vs/editor/test/common/modes/supports/javascriptOnEnterRules'; |
| 14 | +import { DisposableStore } from 'vs/base/common/lifecycle'; |
| 15 | +import { ILanguageConfigurationService } from 'vs/editor/common/languages/languageConfigurationRegistry'; |
| 16 | +import { ILanguageService } from 'vs/editor/common/languages/language'; |
| 17 | +import { EncodedTokenizationResult, IState, TokenizationRegistry } from 'vs/editor/common/languages'; |
| 18 | +import { MetadataConsts, StandardTokenType } from 'vs/editor/common/encodedTokenAttributes'; |
| 19 | +import { createTextModel } from 'vs/editor/test/common/testTextModel'; |
| 20 | +import { NullState } from 'vs/editor/common/languages/nullTokenize'; |
9 | 21 |
|
10 | 22 | function testIndentationToSpacesCommand(lines: string[], selection: Selection, tabSize: number, expectedLines: string[], expectedSelection: Selection): void {
|
11 | 23 | testCommand(lines, null, selection, (accessor, sel) => new IndentationToSpacesCommand(sel, tabSize), expectedLines, expectedSelection);
|
@@ -184,3 +196,72 @@ suite('Editor Contrib - Indentation to Tabs', () => {
|
184 | 196 | );
|
185 | 197 | });
|
186 | 198 | });
|
| 199 | + |
| 200 | +suite('Editor Contrib - Auto Indent On Paste', () => { |
| 201 | + let disposables: DisposableStore; |
| 202 | + |
| 203 | + setup(() => { |
| 204 | + disposables = new DisposableStore(); |
| 205 | + }); |
| 206 | + |
| 207 | + teardown(() => { |
| 208 | + disposables.dispose(); |
| 209 | + }); |
| 210 | + |
| 211 | + test('issue #119225: Do not add extra leading space when pasting JSDoc', () => { |
| 212 | + const languageId = 'leadingSpacePaste'; |
| 213 | + const model = createTextModel("", languageId, {}); |
| 214 | + disposables.add(model); |
| 215 | + withTestCodeEditor(model, { autoIndent: 'full' }, (editor, viewModel, instantiationService) => { |
| 216 | + const languageService = instantiationService.get(ILanguageService); |
| 217 | + const languageConfigurationService = instantiationService.get(ILanguageConfigurationService); |
| 218 | + disposables.add(languageService.registerLanguage({ id: languageId })); |
| 219 | + disposables.add(TokenizationRegistry.register(languageId, { |
| 220 | + getInitialState: (): IState => NullState, |
| 221 | + tokenize: () => { |
| 222 | + throw new Error('not implemented'); |
| 223 | + }, |
| 224 | + tokenizeEncoded: (line: string, hasEOL: boolean, state: IState): EncodedTokenizationResult => { |
| 225 | + const tokensArr: number[] = []; |
| 226 | + if (line.indexOf('*') !== -1) { |
| 227 | + tokensArr.push(0); |
| 228 | + tokensArr.push(StandardTokenType.Comment << MetadataConsts.TOKEN_TYPE_OFFSET); |
| 229 | + } else { |
| 230 | + tokensArr.push(0); |
| 231 | + tokensArr.push(StandardTokenType.Other << MetadataConsts.TOKEN_TYPE_OFFSET); |
| 232 | + } |
| 233 | + const tokens = new Uint32Array(tokensArr.length); |
| 234 | + for (let i = 0; i < tokens.length; i++) { |
| 235 | + tokens[i] = tokensArr[i]; |
| 236 | + } |
| 237 | + return new EncodedTokenizationResult(tokens, state); |
| 238 | + } |
| 239 | + })); |
| 240 | + disposables.add(languageConfigurationService.register(languageId, { |
| 241 | + brackets: [ |
| 242 | + ['{', '}'], |
| 243 | + ['[', ']'], |
| 244 | + ['(', ')'] |
| 245 | + ], |
| 246 | + comments: { |
| 247 | + lineComment: '//', |
| 248 | + blockComment: ['/*', '*/'] |
| 249 | + }, |
| 250 | + indentationRules: javascriptIndentationRules, |
| 251 | + onEnterRules: javascriptOnEnterRules |
| 252 | + })); |
| 253 | + |
| 254 | + const autoIndentOnPasteController = editor.registerAndInstantiateContribution(AutoIndentOnPaste.ID, AutoIndentOnPaste); |
| 255 | + const pasteText = [ |
| 256 | + '/**', |
| 257 | + ' * JSDoc', |
| 258 | + ' */', |
| 259 | + 'function a() {}' |
| 260 | + ].join('\n'); |
| 261 | + |
| 262 | + viewModel.paste(pasteText, true, undefined, 'keyboard'); |
| 263 | + autoIndentOnPasteController.trigger(new Range(1, 1, 4, 16)); |
| 264 | + assert.strictEqual(model.getValue(), pasteText); |
| 265 | + }); |
| 266 | + }); |
| 267 | +}); |
0 commit comments