Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unwanted extra spaces when pasting code with JSDoc #136579

Merged
merged 7 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 31 additions & 21 deletions src/vs/editor/common/languages/autoIndent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,33 +228,43 @@ export function getGoodIndentForLine(
if (indent) {
const inheritLine = indent.line;
if (inheritLine !== undefined) {
const enterResult = richEditSupport.onEnter(autoIndent, '', virtualModel.getLineContent(inheritLine), '');
// Apply enter action as long as there are only whitespace lines between inherited line and this line.
let shouldApplyEnterRules = true;
for (let inBetweenLine = inheritLine; inBetweenLine < lineNumber - 1; inBetweenLine++) {
if (!/^\s*$/.test(virtualModel.getLineContent(inBetweenLine))) {
shouldApplyEnterRules = false;
break;
}
}
if (shouldApplyEnterRules) {
const enterResult = richEditSupport.onEnter(autoIndent, '', virtualModel.getLineContent(inheritLine), '');

if (enterResult) {
let indentation = strings.getLeadingWhitespace(virtualModel.getLineContent(inheritLine));
if (enterResult) {
let indentation = strings.getLeadingWhitespace(virtualModel.getLineContent(inheritLine));

if (enterResult.removeText) {
indentation = indentation.substring(0, indentation.length - enterResult.removeText);
}
if (enterResult.removeText) {
indentation = indentation.substring(0, indentation.length - enterResult.removeText);
}

if (
(enterResult.indentAction === IndentAction.Indent) ||
(enterResult.indentAction === IndentAction.IndentOutdent)
) {
indentation = indentConverter.shiftIndent(indentation);
} else if (enterResult.indentAction === IndentAction.Outdent) {
indentation = indentConverter.unshiftIndent(indentation);
}
if (
(enterResult.indentAction === IndentAction.Indent) ||
(enterResult.indentAction === IndentAction.IndentOutdent)
) {
indentation = indentConverter.shiftIndent(indentation);
} else if (enterResult.indentAction === IndentAction.Outdent) {
indentation = indentConverter.unshiftIndent(indentation);
}

if (indentRulesSupport.shouldDecrease(lineContent)) {
indentation = indentConverter.unshiftIndent(indentation);
}
if (indentRulesSupport.shouldDecrease(lineContent)) {
indentation = indentConverter.unshiftIndent(indentation);
}

if (enterResult.appendText) {
indentation += enterResult.appendText;
}
if (enterResult.appendText) {
indentation += enterResult.appendText;
}

return strings.getLeadingWhitespace(indentation);
return strings.getLeadingWhitespace(indentation);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/contrib/indentation/browser/indentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ export class AutoIndentOnPaste implements IEditorContribution {
}));
}

private trigger(range: Range): void {
public trigger(range: Range): void {
const selections = this.editor.getSelections();
if (selections === null || selections.length > 1) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as assert from 'assert';
import { Selection } from 'vs/editor/common/core/selection';
import { IndentationToSpacesCommand, IndentationToTabsCommand } from 'vs/editor/contrib/indentation/browser/indentation';
import { Range } from 'vs/editor/common/core/range';
import { AutoIndentOnPaste, IndentationToSpacesCommand, IndentationToTabsCommand } from 'vs/editor/contrib/indentation/browser/indentation';
import { testCommand } from 'vs/editor/test/browser/testCommand';
import { withTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor';
import { javascriptIndentationRules } from 'vs/editor/test/common/modes/supports/javascriptIndentationRules';
import { javascriptOnEnterRules } from 'vs/editor/test/common/modes/supports/javascriptOnEnterRules';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { ILanguageConfigurationService } from 'vs/editor/common/languages/languageConfigurationRegistry';
import { ILanguageService } from 'vs/editor/common/languages/language';
import { EncodedTokenizationResult, IState, TokenizationRegistry } from 'vs/editor/common/languages';
import { MetadataConsts, StandardTokenType } from 'vs/editor/common/encodedTokenAttributes';
import { createTextModel } from 'vs/editor/test/common/testTextModel';
import { NullState } from 'vs/editor/common/languages/nullTokenize';

function testIndentationToSpacesCommand(lines: string[], selection: Selection, tabSize: number, expectedLines: string[], expectedSelection: Selection): void {
testCommand(lines, null, selection, (accessor, sel) => new IndentationToSpacesCommand(sel, tabSize), expectedLines, expectedSelection);
Expand Down Expand Up @@ -184,3 +196,72 @@ suite('Editor Contrib - Indentation to Tabs', () => {
);
});
});

suite('Editor Contrib - Auto Indent On Paste', () => {
let disposables: DisposableStore;

setup(() => {
disposables = new DisposableStore();
});

teardown(() => {
disposables.dispose();
});

test('issue #119225: Do not add extra leading space when pasting JSDoc', () => {
const languageId = 'leadingSpacePaste';
const model = createTextModel("", languageId, {});
disposables.add(model);
withTestCodeEditor(model, { autoIndent: 'full' }, (editor, viewModel, instantiationService) => {
const languageService = instantiationService.get(ILanguageService);
const languageConfigurationService = instantiationService.get(ILanguageConfigurationService);
disposables.add(languageService.registerLanguage({ id: languageId }));
disposables.add(TokenizationRegistry.register(languageId, {
getInitialState: (): IState => NullState,
tokenize: () => {
throw new Error('not implemented');
},
tokenizeEncoded: (line: string, hasEOL: boolean, state: IState): EncodedTokenizationResult => {
const tokensArr: number[] = [];
if (line.indexOf('*') !== -1) {
tokensArr.push(0);
tokensArr.push(StandardTokenType.Comment << MetadataConsts.TOKEN_TYPE_OFFSET);
} else {
tokensArr.push(0);
tokensArr.push(StandardTokenType.Other << MetadataConsts.TOKEN_TYPE_OFFSET);
}
const tokens = new Uint32Array(tokensArr.length);
for (let i = 0; i < tokens.length; i++) {
tokens[i] = tokensArr[i];
}
return new EncodedTokenizationResult(tokens, state);
}
}));
disposables.add(languageConfigurationService.register(languageId, {
brackets: [
['{', '}'],
['[', ']'],
['(', ')']
],
comments: {
lineComment: '//',
blockComment: ['/*', '*/']
},
indentationRules: javascriptIndentationRules,
onEnterRules: javascriptOnEnterRules
}));

const autoIndentOnPasteController = editor.registerAndInstantiateContribution(AutoIndentOnPaste.ID, AutoIndentOnPaste);
const pasteText = [
'/**',
' * JSDoc',
' */',
'function a() {}'
].join('\n');

viewModel.paste(pasteText, true, undefined, 'keyboard');
autoIndentOnPasteController.trigger(new Range(1, 1, 4, 16));
assert.strictEqual(model.getValue(), pasteText);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

export const javascriptIndentationRules = {
decreaseIndentPattern: /^((?!.*?\/\*).*\*\/)?\s*[\}\]].*$/,
increaseIndentPattern: /^((?!\/\/).)*(\{([^}"'`]*|(\t|[ ])*\/\/.*)|\([^)"'`]*|\[[^\]"'`]*)$/,
// e.g. * ...| or */| or *-----*/|
unIndentedLinePattern: /^(\t|[ ])*[ ]\*[^/]*\*\/\s*$|^(\t|[ ])*[ ]\*\/\s*$|^(\t|[ ])*[ ]\*([ ]([^\*]|\*(?!\/))*)?$/
};