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

Add support for capture groups in onEnterRules #17281

Open
alexdima opened this issue Dec 15, 2016 · 5 comments
Open

Add support for capture groups in onEnterRules #17281

alexdima opened this issue Dec 15, 2016 · 5 comments
Assignees
Labels
editor-autoindent Editor auto indentation issues editor-input Editor text input feature-request Request for new features or functionality
Milestone

Comments

@alexdima
Copy link
Member

alexdima commented Dec 15, 2016

From @siegebell in #3088 (comment)


Before:

// comment
//     indented|

After:

// comment
//     indented
//     |

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 indent
            beforeText: /^\s*(\/\/\s*)\S.*$/,
            action: { indentAction: IndentAction.None, appendText: '$1' }
        }
    ]
});
@alexdima
Copy link
Member Author

alexdima commented Feb 3, 2021

cc @jrieken

I would like to look into this in February, if I have the chance. But I would like to have it aligned with the way snippets do variables. From what I could tell snippets use both $VARIABLE_NAME syntax and ${VARIABLE_NAME} syntax. There is also a question of ambiguities in case afterText or previousLineText are used... which capture group is appendText referring to... The way out of that would be to number capture groups based on the text appearence, so the capture groups in previousLineText would get the first N values, the capture groups in beforeText would get the next M values, and finally the capture groups in afterText would get the next O values. Finally, the question is if this should attempt to support the transform operations of snippets, but I would leave that out for now.

@jrieken
Copy link
Member

jrieken commented Feb 3, 2021

From what I could tell snippets use both $VARIABLE_NAME syntax and ${VARIABLE_NAME} syntax.

Yes, but a variable name is [_a-zA-Z], no numbers. That means ${1} from the sample isn't a variable but a tab-stop unless it is used in variable- or placeholder-transformations where it refers to regexp matching groups. But for that it must be embedded inside the complex transform syntax.

@alexdima
Copy link
Member Author

alexdima commented Feb 3, 2021

To clarify: I don't intend that pressing Enter would bring the user into snippet mode. This is more about capturing the indentation of the line via a capture group and then reusing it in appendText. I initially thought about the find/replace widget, where we use $1, $2, etc. and maybe that is a good enough syntax. There is no need to cause confusion with ${1}, ${2}, etc.

So I would just go with $1, $2...

@jrieken
Copy link
Member

jrieken commented Feb 4, 2021

Right, using $1 (no curly brackets) is a good fit

@alexdima alexdima modified the milestones: February 2021, March 2021 Feb 25, 2021
@alexdima alexdima modified the milestones: March 2021, April 2021 Mar 24, 2021
@alexdima alexdima modified the milestones: April 2021, May 2021 Apr 26, 2021
@alexdima alexdima assigned alexdima and unassigned alexdima May 3, 2021
@hediet
Copy link
Member

hediet commented May 3, 2021

Before:

// comment
//     indented|

After:

// comment
//     indented
//     |

For me, there are two indentations in this example.
The indentation of the comment and the indentation of the text within the comment.
I find it strange to use two different strategies to handle these indentations (explicit indent/dedent/none actions of the onEnter rule vs regex whitespace group references in the insert text).

Is there a scenario where it makes sense to reference a regex group that matches non-whitespace text?

Also, should indentation/dedentation be supported for nested indentations (rather than just keeping the indentation as is)?

Maybe it might also be valuable to consider embedded languages in general. Imagine using a markdown codeblock in doc-comments:

/**
 * Example:
 * ```ts
 * class Foo { <<User types enter here>>
 *
 * ```
 *
 */
function bar() {}

Ideally, the outer and inner typescript rules would be combined somehow, yielding this:

/**
 * Example:
 * ```ts
 * class Foo {
 *     <<New Cursor Position>>
 *
 * ```
 *
 */
function bar() {}

Currently VS Code ignores the rules of the inner language.

This feature would not fix #66235 anyway.

I think this feature might require more discussions.
Until then, it could be investigated how parts of this feature can be implemented by extensions listening on onDidChangeTextDocument and issuing edits and selection updates instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editor-autoindent Editor auto indentation issues editor-input Editor text input feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests

4 participants