-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Adding an overload to TextEditor.edit for insertion of a SnippetString #17628
Conversation
More robust type validation on ext side of insertSnippet. Position/range check for snippet insertion was inverted. Adding insertSnippet to vscode.d.ts. (Should it be in vscode.proposed.d.ts?) Adding extension API tests for insertSnippet.
Hi @joelday, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution! TTYL, MSBOT; |
@jrieken Looks like vscode-api-tests fails because it relies on the published vscode.d.ts. Was manually replacing that during development. |
Hi, I am closing and re-opening this PR to bump the CLA bot. Sorry for the inconvenience! |
Hi @joelday, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution! TTYL, MSBOT; |
@joelday Ping? Are you still looking into this? Anything thing I can help you with? |
@jrieken should work as-is. Unless the implementation needs changes or the test needs to be more thorough (I just realized that it should probably be checking for snippet mode), I'd guess that what is left is to sign off on the API and documentation language. |
@jrieken unsure of the right way to resolve the test failures for unpublished changes to vscode.d.ts. |
I had left some review comments that require some changes |
@jrieken Weird, I can't find them/didn't get any notifications. |
* @return A promise that resolves with a value indicating if the snippet could be inserted. | ||
*/ | ||
insertSnippet(template: string, posOrRange: Position | Range): Thenable<boolean>; | ||
} |
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.
this isn't needed as we make the change directly in vscode.d.ts
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 didn't think so, but the extensions project pulls from the publicly published vscode package instead of the vscode.d.ts in the main project. That's why I got the first build failure in the PR.
* @param posOrRange The position or replacement range representing the location of the insertion. | ||
* @return A promise that resolves with a value indicating if the snippet could be inserted. | ||
*/ | ||
insertSnippet(template: string, posOrRange: Position | Range): Thenable<boolean>; |
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.
- template should be of type
SnippetString
- Unsure about
posOrRange
- we should consider using the current editor selection/selections. That would allow to insert snippets in multiple locations at the same time. Also inserting snippets is highly interactive, so it's fair to use the current selection for that (or change the selection first). - We should consider making this an overload of the
edit
method
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.
Agreed that you should be able to call this without being concerned with location. Sharing an argument for both position or range seems a little awkward as well. So long as I can alter the selection before starting. How should this work for multiple selections?
In the case of overloading TextEditor.edit
, if that involves changes to TextEditorEdit
, how does entering snippet mode fit in with subsequent insertions, replacements, etc.? It seems like this might have more in common with CompletionItem
than editing.
Another thought: Should we supply a callback to know when snippet mode was exited?
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.
How should this work for multiple selections?
As with a single selection, the editor API allows to set one or many selections. The snippet logic tho isn't yet smart enough to always honour multiple cursors when snippets are inserted. We have a issue for this and it will be implemented in the near future
how does entering snippet mode fit in with subsequent insertions, replacements, etc.? It seems like this might have more in common with CompletionItem than editing.
Sure, I was just wondering if reusing the name edit
makes sense because that is sort of what we do. It doesn't have to fit in the builder but could just reuse the name, like so:
edit(snippet: SnippetString, options?:{/*undo stops control*/}): void;
edit(callback: (editBuilder: TextEditorEdit) => void, options?: { undoStopBefore: boolean; undoStopAfter: boolean; }): Thenable<boolean>;
Another thought: Should we supply a callback to know when snippet mode was exited?
Not a fan because it really depends on the user tabbing through things to exit snippet mode.
public insertSnippet(template: string, overwriteBefore: number, overwriteAfter: number): void { | ||
const snippet = CodeSnippet.fromTextmate(template, this._variableResolver); | ||
this.run(snippet, overwriteBefore, overwriteAfter); | ||
} | ||
|
||
public insertSnippetWithReplaceRange(template: string, replaceRange: Range): void { |
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 should try to get away with just using insertSnippet
. The controller is already complex
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.
Agreed that this could be consolidated.
Oh, maybe I didn't press some buttons here... Should be in now |
@jrieken Sorry for the late responses. |
@jrieken I'll experiment with this tonight based on your feedback. Thanks! |
…ng when already in snippet mode.
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.
Really good. Left some nit comments. I will also think about if edit
should return a promise or not. Initially I was against it but maybe it should resolve as soon the snippet is inserted, e.g. the document is updated... Unsure yet
setEndOfLine: EndOfLine; | ||
} | ||
|
||
export interface IInsertSnippetOptions extends IUndoStopOptions { | ||
|
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.
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.
Ha. Noted.
*/ | ||
edit(snippet: SnippetString, options?: { undoStopBefore: boolean; undoStopAfter: boolean; }): void; | ||
|
||
} |
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.
It is enough to have it vscode.d.ts
only. Duplication not needed
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.
Should editor-api-tests be changed to point to the local vscode.d.ts
instead of pulling it from https://raw.githubusercontent.com/Microsoft/vscode/master/src/vs/vscode.d.ts
at build time?
@jrieken, I can have it return a promise again, just let me know. |
- Basing snippet insertion failure on a new `_codeEditor` null-check. - Now returns `Thenable<boolean>`. - Removed vscode.proposed.d.ts copy of the `TextEditor` change. - Removing empty options interface.
@jrieken Made a few changes. Thank you! |
@@ -392,9 +388,13 @@ export class MainThreadTextEditor { | |||
return false; | |||
} | |||
|
|||
insertSnippet(template: string, opts: IInsertSnippetOptions) { | |||
insertSnippet(template: string, opts: IUndoStopOptions) { |
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.
@jrieken Should console.warn('applyEdits on invisible editor');
be added to insertSnippet
?
TextEditor.edit
for insertion of a SnippetString
TextEditor.edit
for insertion of a SnippetString
I have fixed the compile issue with |
@jrieken I'm really excited about having made this kind of contribution! The surface of external APIs is a pretty big commitment. This is a huge win for joelday.docthis and extensions like it. Thank you for the critical eye. |
Implementation of #15952. Let me know if there's anything I can improve/fix or if the desired API and behavior should change. Thanks!