Skip to content

fix #13945. support format on paste - #18476

Merged
Alexandru Dima (alexdima) merged 4 commits into
microsoft:masterfrom
rebornix:FormatOnPaste
Jan 19, 2017
Merged

Alexandru Dima (alexdima) merged 4 commits into
microsoft:masterfrom
rebornix:FormatOnPaste

Conversation

@rebornix

Copy link
Copy Markdown
Contributor

Invoke format selection immediately after pasting event.

@jrieken Johannes Rieken (jrieken) left a comment

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe 'Paste and Format'?

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor

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

Peng Lyu (rebornix) commented Jan 13, 2017 •

Copy link
Copy Markdown
Contributor Author

Johannes Rieken (@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
Copy Markdown
Contributor

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
Copy Markdown
Contributor Author

Johannes Rieken (@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
Copy Markdown
Contributor

Code looks good to me. Waiting for alexandrudima to review

@alexdima Alexandru Dima (alexdima) left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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.

* An event emitted when users paste text in the editor.
* @event
* @internal
*/

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor Author

alexandrudima comments addressed.

@alexdima

Copy link
Copy Markdown
Member

Brilliant!

@alexdima
Alexandru Dima (alexdima) merged commit abbd2a7 into microsoft:master Jan 19, 2017
@alexdima

Copy link
Copy Markdown
Member

Peng Lyu (@rebornix) remember to create a test plan item for this

@jrieken

Copy link
Copy Markdown
Contributor

Peng Lyu (@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.

4 participants