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 #13945. support format on paste #18476

Merged
merged 4 commits into from
Jan 19, 2017
Merged

Conversation

rebornix
Copy link
Member

Invoke format selection immediately after pasting event.

Copy link
Member

@jrieken jrieken left a comment

Choose a reason for hiding this comment

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

Interesting approach. I was always thinking about a paste-listener and a setting but I actually like this. Left some nit comments, mostly around keybindings/menus

super({
id: 'editor.action.formatOnPaste',
label: nls.localize('formatOnPaste.label', "Format on paste"),
alias: 'Format on paste',
Copy link
Member

Choose a reason for hiding this comment

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

Maybe 'Paste and Format'?

id: 'editor.action.formatOnPaste',
label: nls.localize('formatOnPaste.label', "Format on paste"),
alias: 'Format on paste',
precondition: EditorContextKeys.Writable
Copy link
Member

Choose a reason for hiding this comment

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

We have context key which knows if for the current document there is someone that can format selections: ModeContextKeys.hasDocumentSelectionFormattingProvider (see FormatSelection as a sample). With that the menus will automatically update.

id: 'editor.action.formatOnPaste',
label: nls.localize('formatOnPaste.label', "Format on paste"),
alias: 'Format on paste',
precondition: EditorContextKeys.Writable
Copy link
Member

Choose a reason for hiding this comment

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

Also register a keybinding and a menu item for this

@rebornix
Copy link
Member Author

rebornix commented Jan 13, 2017

@jrieken addressed your comments. Bind key chord cmd+k cmd+v to this action as there aren't many choices for simple keybindings.

@jrieken
Copy link
Member

jrieken commented Jan 16, 2017

On a second thought I am not so sure anymore if a separate action like 'Format and Paste' is the best solution. These are my concerns

  • Having two actions I wonder if the usage patterns would become confusing. I might be a formatter-fan and bind cmd+v to 'paste&format' just to learn that paste is defunct for languages that don't have a formatter. That would force users to know if a formatter is available and then use a different keybinding.
  • We already have settings like formatOnType and formatOnSave and I believe it would feel more natural to also have formatOnPaste

@rebornix
Copy link
Member Author

@jrieken while implementing the command I have a feeling that ppl might prefer it as an option as well, I like formatOnPaste. I've already updated the code and follow the way we implement formatOnType.

@jrieken
Copy link
Member

jrieken commented Jan 18, 2017

Code looks good to me. Waiting for @alexandrudima to review

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.

Looks good overall, just a few details left.

this.cursor.trigger(source, handlerId, payload);
const endPosition = this.cursor.getSelection().getStartPosition();
if (source === 'keyboard') {
this.emit(editorCommon.EventType.DidPaste, {
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer for the editor to always give out rich objects (i.e. Range and not IRange). It does so for all non-serializable events.

* @event
* @internal
*/
onDidPaste(listener: (range: Range) => void): IDisposable;
Copy link
Member

Choose a reason for hiding this comment

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

Here we say we give out a Range, but the implementation gives out an IRange. Please adjust the implementation.

return;
}

var model = this.editor.getModel();
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: prefer let over var (same two lines below)


// no support
var [support] = OnTypeFormattingEditProviderRegistry.ordered(model);
if (!support || !support.autoFormatTriggerCharacters) {
Copy link
Member

Choose a reason for hiding this comment

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

No need for the support to have autoFormatTriggerCharacters. Perhaps a copy-paste error?

Copy link
Member

Choose a reason for hiding this comment

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

Now that I look at it, we should not read the OnTypeFormattingEditProvierRegistry, but the DocumentRangeFormatting bla bla Registry.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup it's a stupid copy-paste error, I made changes to one place but forgot it here.

}
const command = new EditOperationsCommand(edits, this.editor.getSelection());
this.editor.executeCommand(this.getId(), command);
this.editor.focus();
Copy link
Member

Choose a reason for hiding this comment

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

No need to focus here, since we are not moving the focus anywhere.

@rebornix
Copy link
Member Author

@alexandrudima comments addressed.

@alexdima
Copy link
Member

Brilliant!

@alexdima alexdima merged commit abbd2a7 into microsoft:master Jan 19, 2017
@alexdima alexdima added this to the January 2017 milestone Jan 23, 2017
@alexdima
Copy link
Member

@rebornix remember to create a test plan item for this

@jrieken
Copy link
Member

jrieken commented Jan 23, 2017

@rebornix @alexandrudima i have done that

@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

4 participants