-
Notifications
You must be signed in to change notification settings - Fork 29.4k
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
Conversation
There was a problem hiding this 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', |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
b8631fb
to
3f3ea06
Compare
@jrieken addressed your comments. Bind key chord |
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
|
@jrieken while implementing the command I have a feeling that ppl might prefer it as an option as well, I like |
Code looks good to me. Waiting for @alexandrudima to review |
There was a problem hiding this 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, { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
@alexandrudima comments addressed. |
Brilliant! |
@rebornix remember to create a test plan item for this |
@rebornix @alexandrudima i have done that |
Invoke format selection immediately after pasting event.