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

Adding an overload to TextEditor.edit for insertion of a SnippetString #17628

Merged
merged 13 commits into from
Jan 20, 2017
Merged

Adding an overload to TextEditor.edit for insertion of a SnippetString #17628

merged 13 commits into from
Jan 20, 2017

Conversation

joelday
Copy link
Contributor

@joelday joelday commented Dec 20, 2016

Implementation of #15952. Let me know if there's anything I can improve/fix or if the desired API and behavior should change. Thanks!

joelday and others added 4 commits December 16, 2016 00:50
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.
@msftclas
Copy link

Hi @joelday, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!

In order for us to evaluate and accept your PR, we ask that you sign a contribution license agreement. It's all electronic and will take just minutes. I promise there's no faxing. https://cla.microsoft.com.

TTYL, MSBOT;

@joelday
Copy link
Contributor Author

joelday commented Dec 20, 2016

@jrieken Looks like vscode-api-tests fails because it relies on the published vscode.d.ts. Was manually replacing that during development.

@msftgits
Copy link

Hi, I am closing and re-opening this PR to bump the CLA bot. Sorry for the inconvenience!

@msftgits msftgits closed this Dec 20, 2016
@msftgits msftgits reopened this Dec 20, 2016
@msftclas
Copy link

Hi @joelday, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
You've already signed the contribution license agreement. Thanks!
We will now validate the agreement and then real humans will evaluate your PR.

TTYL, MSBOT;

@jrieken jrieken added this to the January 2017 milestone Dec 21, 2016
@jrieken
Copy link
Member

jrieken commented Jan 9, 2017

@joelday Ping? Are you still looking into this? Anything thing I can help you with?

@joelday
Copy link
Contributor Author

joelday commented Jan 9, 2017

@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.

@joelday
Copy link
Contributor Author

joelday commented Jan 9, 2017

@jrieken unsure of the right way to resolve the test failures for unpublished changes to vscode.d.ts.

@jrieken
Copy link
Member

jrieken commented Jan 10, 2017

I had left some review comments that require some changes

@joelday
Copy link
Contributor Author

joelday commented Jan 10, 2017

@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>;
}
Copy link
Member

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

Copy link
Contributor Author

@joelday joelday Jan 13, 2017

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>;
Copy link
Member

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

Copy link
Contributor Author

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?

Copy link
Member

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 {
Copy link
Member

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

Copy link
Contributor Author

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.

@jrieken
Copy link
Member

jrieken commented Jan 11, 2017

Oh, maybe I didn't press some buttons here... Should be in now

@joelday
Copy link
Contributor Author

joelday commented Jan 13, 2017

@jrieken Sorry for the late responses.

@joelday
Copy link
Contributor Author

joelday commented Jan 17, 2017

@jrieken I'll experiment with this tonight based on your feedback. Thanks!

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.

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 {

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

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;

}
Copy link
Member

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

Copy link
Contributor Author

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?

@joelday
Copy link
Contributor Author

joelday commented Jan 18, 2017

@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.
@joelday
Copy link
Contributor Author

joelday commented Jan 18, 2017

@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) {
Copy link
Contributor Author

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?

@joelday joelday changed the title Adding an insertSnippet method to the TextEditor extension API. Adding an overload to TextEditor.edit for insertion of a SnippetString Jan 18, 2017
@joelday joelday changed the title Adding an overload to TextEditor.edit for insertion of a SnippetString Adding an overload to TextEditor.edit for insertion of a SnippetString Jan 18, 2017
@jrieken
Copy link
Member

jrieken commented Jan 20, 2017

I have fixed the compile issue with vscode-api-tests and this is now ready to be merged. Thanks for your contribution and for the patience!

@jrieken jrieken merged commit b4817e5 into microsoft:master Jan 20, 2017
@joelday
Copy link
Contributor Author

joelday commented Jan 20, 2017

@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.

@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

5 participants