You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Position of the cursor after sending []TextEdit through provideOnTypeFormattingEdits #3088
export class GoCommentsProvider implements OnTypeFormattingEditProvider {
public provideOnTypeFormattingEdits(document: TextDocument, position: Position, ch: string, options: FormattingOptions, token: CancellationToken): Thenable<TextEdit[]>{
//Detect if we are in a case of comments
return new Promise((resolve, reject) => {
let previousLine = document.lineAt(position.line - 1);
if (previousLine.text.startsWith("//")) {
let commentedAt = TextEdit.insert(position, "//");
resolve([commentedAt]);
}
});
}
}
The problem here is that the final position of the cursor is the following:
// First line of comment -> Press entry
|// Cursor is located before the addition
What I would like to achieve:
// First line of comment -> Press entry
// | Cursor is located after the addition
Yannick Heinrich (@yageek) IMHO this does not really fit a format edit. A format edit should do transformations that do not affect the program logic (mostly adding/removing whitespace). Maybe what you want can be achieved (a lot easier) with onEnterRules?:
Could be. I saw in the API that there is also an afterText condition. Is it possible to use both conditions at the same time ?
I would like to perform this addition but only if the next line contains some specific keywords.
May be I'm too bad at Regex, but I can not manage to find the appropriate afterText Regex.
For example, with a try for the type keyword, I put: afterText: /\s*\ntype.*/g,
If your logic doesn't match the onEnterRules, you could alternatively listen to onDidChangeTextDocument, figure out if the changed document is the one in the activeTextEditor and then use activeTextEditor.edit(() => ...).then(() => activeTextEditor.selection = ...)
Closing this for now as it is unclear to me what is the use case. If none of our concepts fit the use case, an extension could register a keybinding rule for 'Enter' and do all the logic for 'Enter' on its own.
For this case, onEnterRules could be extended to support matching groups like find/replace currently does...
languages.setLanguageConfiguration(modeID,{onEnterRules: [{// insert comment on new line while preserving the indentbeforeText: /^\s*(\/\/\s*)\S.*$/,action: {indentAction: IndentAction.None,appendText: '${1}'}}]});
CJ Bell (@siegebell) I found the idea to support capture groups really good. I've opened a new issue #17281 to track this. The idea to support a callback is IMHO unrealistic because the callback is defined in the extension host process and the handling of pressing enter and of the rules is running on the renderer process (i.e. cannot invoke appendText synchronously, as part of handling the Enter keypress)
alexandrudima I agree - It seems to me that the existence of onEnterRules is to facilitate a snappy text-editing experience, so if it had to wait for a Thenable callback there would be no advantage over onDidChangeTextDocument.
That said, here's another use-case (3):
constx='some '+'long '+'string ';
and (4)
constx='some '+'long '+'string ';
Maybe there needs to be a more flexible way to specify an alignment point. For example:
onEnterRules: [{// case (3): align indent to after the prior-line's = or + if it is not terminated by ';' or is terminated by +, ||, or &&beforeText: /^.*(?:=|\+)\s*.*(?:[^;]|[+]|\|\||&&)$/,action: {indentAction: vscode.IndentAction.Align,alignTo: /^.*=\s*(.)/}},{// case (4): align indent to prior-line's = or + if it is terminated by a string and no ';'beforeText: /^.*(?:=|\+)\s*.*['"`]\s*$/,action: {indentAction: vscode.IndentAction.Align,alignTo: /=\+/,appendText: '+ '}},{// case (1): align indent to prior-line's tacticbeforeText: /^\s*\*+\s*\S.*$/,action: {indentAction: vscode.IndentAction.Align,alignTo: /^\s*\*+\s(\S)/}}]
The behavior of alignTo: x would be to either indent to the point where x is first matched or to the beginning of the first matched group if x is a regular expression with capturing groups.
Activity
alexdima commentedon Feb 22, 2016
Yannick Heinrich (@yageek) IMHO this does not really fit a format edit. A format edit should do transformations that do not affect the program logic (mostly adding/removing whitespace). Maybe what you want can be achieved (a lot easier) with
onEnterRules?:yageek commentedon Feb 22, 2016
Could be. I saw in the API that there is also an
afterTextcondition. Is it possible to use both conditions at the same time ?I would like to perform this addition but only if the next line contains some specific keywords.
May be I'm too bad at Regex, but I can not manage to find the appropriate
afterTextRegex.For example, with a try for the
typekeyword, I put:afterText: /\s*\ntype.*/g,but nothing is appended to the source code.
alexdima commentedon Feb 22, 2016
The
afterTextis just about matching the text on the current line after the cursor position:I mean those are the values that are matched against the regexes
alexdima commentedon Feb 22, 2016
If your logic doesn't match the
onEnterRules, you could alternatively listen toonDidChangeTextDocument, figure out if the changed document is the one in theactiveTextEditorand then useactiveTextEditor.edit(() => ...).then(() => activeTextEditor.selection = ...)alexdima commentedon Mar 17, 2016
Closing this for now as it is unclear to me what is the use case. If none of our concepts fit the use case, an extension could register a keybinding rule for 'Enter' and do all the logic for 'Enter' on its own.
siegebell commentedon Dec 15, 2016
alexandrudima Here's a use-case (it's been requested by users of vscoq and is a feature in the equivalent emacs extension; language: coq):
Before:
After
* tactic. |Effect: the cursor is indented to a position that is dependent on the previous line.
siegebell commentedon Dec 15, 2016
alexandrudima another use-case:
Before:
After:
For this case,
onEnterRulescould be extended to support matching groups like find/replace currently does...siegebell commentedon Dec 15, 2016
And to support the use-case for aligning to the tactic after a bullet,
appendTextcould accept a function that takes aRegExpExecArrayas an argument:alexdima commentedon Dec 15, 2016
CJ Bell (@siegebell) I found the idea to support capture groups really good. I've opened a new issue #17281 to track this. The idea to support a callback is IMHO unrealistic because the callback is defined in the extension host process and the handling of pressing enter and of the rules is running on the renderer process (i.e. cannot invoke
appendTextsynchronously, as part of handling the Enter keypress)siegebell commentedon Dec 15, 2016
alexandrudima I agree - It seems to me that the existence of
onEnterRulesis to facilitate a snappy text-editing experience, so if it had to wait for aThenablecallback there would be no advantage overonDidChangeTextDocument.That said, here's another use-case (3):
and (4)
Maybe there needs to be a more flexible way to specify an alignment point. For example:
The behavior of
alignTo: xwould be to either indent to the point wherexis first matched or to the beginning of the first matched group ifxis a regular expression with capturing groups.