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

Add missing Sublime/Atom commands #15787

Merged
merged 8 commits into from
Dec 5, 2016
Merged

Conversation

rebornix
Copy link
Member

@rebornix rebornix commented Nov 21, 2016

Add missing commands from Sublime/Atom per #3776 .

TODO:

  • Join lines
  • Transpose
  • Upper/Lower case
  • Paste and format
  • Test cases

Discussed with Kai and Alex, our contrib part of code is used as internal extension so it's Okay to put these missing commands into our code base. However while implementing them in core, I found that there is one new issue: key bindings conflicts. Will have a discuss with Alex and @waderyan on Monday.

@mention-bot
Copy link

@rebornix, thanks for your PR! By analyzing the history of the files in this pull request, we identified @alexandrudima, @egamma and @jrieken to be potential reviewers.

@wprater
Copy link
Contributor

wprater commented Nov 30, 2016

is paste and indent a larger feature as part of the formatting package? or something you'll add independently of this?

Indenting to the nearest line is not a practical solution, correct?

#4039

@rebornix
Copy link
Member Author

rebornix commented Dec 3, 2016

Add test cases for these commands. Please note that we don't use the name paste and indent as that's reflecting what it really is, instead we use paste and format: paste the content and make sure the pasted content is formatted correctly.

Besides, the formatting of the content is decided by format provider. @wprater .

Copy link
Member

@alexdima alexdima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL;DR I'd remove the formatAndPaste action for now, we can discuss more about it, I would prefer to have the responsibility of the format contribution to format on paste (with an option).

Otherwise, there's a typo that causes bundling errors, and some corner cases.

Nice job overall! :)

import { SortLinesCommand } from 'vs/editor/contrib/linesOperations/common/sortLinesCommand';
import { getDocumentRangeFormattingEdits } from 'vs/editor/contrib/format/common/format';
import { EditOperationsCommand } from 'vs/editor/contrib/format/common//formatCommand';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two slashes here -> vs/editor/contrib/format/common//formatCommand

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

insertSpace = false;
}

if (trimmedLinesContent.charAt(trimmedLinesContent.length - 1) === ' ' ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trimmedLinesContent.length could be 0 here, leading to a call of trimmedLinesContent.charAt(-1).

You could use if (insertSpace && ....)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I relied on charAt(-1) returning empty string but yes it's not a good one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.


let lineTextWithoutIndent = lineText.substr(firstNonWhitespaceIdx - 1);

if (lineTextWithoutIndent.charAt(0) === ')') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we looking for ) in the line text here ? i.e. this looks weird to me, why not for ]. I suggest to remove this unless it is part of some weird join lines spec.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one I followed is Vim http://vimdoc.sourceforge.net/htmldoc/change.html#J but Sublime is doing this for all matching brackets. I think we can do the same, read matching brackets from language config. Right now I'll just remove it.

}

@editorAction
class PasteAndFormatAction extends EditorAction {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎 IMHO on this action.

I would prefer to have an editor.formatOnPaste which in my opinion would align more with our current approach to automatic formatting (same as formatOnType and formatOnSave). @jrieken what do you think?

This one also assumes it knows how paste is implemented in the cursor, but in case of multicursor paste touches multiple ranges, not just one. i.e. pastedContentRange is incorrect in case of multicursor paste.

I suggest to wait with this action and consider implementing it in the format contribution, we are shipping since a long time without it, and there's no reason to rush it in.

let selection = selections[i];
if (selection.isEmpty()) {
let cursor = selection.getStartPosition();
let word = model.getWordAtPosition(cursor);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

word can be null here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

let selection = selections[i];
if (selection.isEmpty()) {
let cursor = selection.getStartPosition();
let word = model.getWordAtPosition(cursor);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

word can be null here. think of empty lines.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

let word = model.getWordAtPosition(cursor);
let wordRange = new Range(cursor.lineNumber, word.startColumn, cursor.lineNumber, word.endColumn);

if (wordRange !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same ??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

}

@editorAction
export class LowerCaseAction extends EditorAction {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to create an abstract CaseAction that has an abstract method _modifyText or whatever that can be overwritten to use toLocaleUpperCase and toLocalLowerCase to avoid code duplication.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored. Now they should look similar to abstract class AbstractFormatAction. Thanks for pointing it out. I was too lazy at that moment.

);
});

test('toggle case', function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add more tests, e.g. for empty lines or for the cursor in the middle of whitespace.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

});
});

test('transpose', function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it work for empty lines, or on empty files?

You can use scripts\test.bat --coverage and my awesome LCOV plugin to see your code coverage ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They work well, added test cases for them as well.

@rebornix
Copy link
Member Author

rebornix commented Dec 5, 2016

@alexandrudima removed paste and indent action and addressed other comments.

@alexdima alexdima merged commit 9fc25c0 into microsoft:master Dec 5, 2016
@alexdima
Copy link
Member

alexdima commented Dec 5, 2016

Nice! LGTM 👍

@thekalinga
Copy link

thekalinga commented Dec 6, 2016

Can the shortcut be added to toggle case along with upper and lowercase, as this makes life easier to have just one shortcut to toggle case.

Which is what IDEA follows, which starts with upper casing everything, followed by lower casing, if you press the same shortcut again.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants