Posted 19 years ago by billb - Software Craftsman, Yye Software
Avatar
It looks like SmartIndent will work fine for an opening brace (in the case of say, C# or C++), however, it's not going to work for the closing brace (since it waits on an ENTER). In VS.NET, the closing brace gets "outdented" as soon as its typed. I'm looking at the SemanticParser events and it looks like I might be able to catch the closing brace, but I'm having a hard time deleting the whitespace (I figured I'd want to delete the token(s) immediately before the closing brace).

I'm just now getting into the gritty details of this control, so any n00b help and direction is appreciated.

Comments (7)

Posted 19 years ago by billb - Software Craftsman, Yye Software
Avatar
I think I figured out it, although I'm not sure if this is the proper way. For brace indenting I handle the SmartIndent function like this:

private void syntaxEditor_SmartIndent(object sender, ActiproSoftware.SyntaxEditor.SmartIndentEventArgs e)
{
    TextStream ts = syntaxEditor.Document.GetTextStream(syntaxEditor.SelectedView.Selection.EndOffset);
    ts.GoToPreviousToken();

    if (ts.CurrentToken.Key == "OpenCurlyBraceToken")
    {
        e.PerformIndent = true;
        e.IndentAmount += 1;
    }
}

Then for the outdent, I do the following in the DocumentTextChanged

private void syntaxEditor_DocumentTextChanged(object sender, ActiproSoftware.SyntaxEditor.DocumentModificationEventArgs e)
{
    switch(e.Modification.InsertedText)
    {
        case "}":
            TextStream ts = syntaxEditor.Document.GetTextStream(syntaxEditor.SelectedView.Selection.EndOffset);
            ts.GoToPreviousToken();

            if (ts.CurrentToken.Key == "CloseCurlyBraceToken")
            {
                syntaxEditor.SelectedView.Selection.SelectToPreviousWord();
                syntaxEditor.SelectedView.Outdent();
            }
            break;
    }
}

I hope this is useful to others.
Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Once we get version 3.0 out, we're going to focus on better parsing for the next version. If you have any ideas on how you'd like to improve smart formatting, please post them.


Actipro Software Support

Posted 19 years ago by billb - Software Craftsman, Yye Software
Avatar
I think I'm still too green to make any sort of well thought out suggestions. However, I'm going to go down the path of writing full blown intellisense for C# and VB.NET. I'm sure after that whole ordeal, I'll have plenty of suggestions.

In the meantime, I need all the help we can get.

Is there a hard date for 3.0?
Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
No hard date yet but beta testing will begin soon. It's mostly complete.


Actipro Software Support

Posted 19 years ago by tobias weltner
Avatar
I am also new to this control but from what I know about other controls, I'd suggest smart indent should work from the level of language definition.
For a token, there should be a property that determines indentation and also specifies whether it should occur before or after the token.
For example, when I enter a "For" statement, I'd expect the next line to be indented, and once I enter "Next" or "Exit For", I'd expect indenting backwards including the current line.

I know now how to manage indenting completely manually, but I doubt this to be an efficient way. I would need to write pages of code to care for all the possible situations, and also I do like the concept of storing all the language-related stuff inside a language definition.

Or did I overlook something, and there IS already a language definition that cares for all of this?
Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
We still have that on our TODO list. Part of it is that you need more than just lexical information to determine when to indent/outdent. For instance, in C#, a } could be a code block close or an array declaration end. The one causes an outdent while the other does not.

So what we're doing is trying to come up with some language-specific parsing code for the future that gives more semantic data. Using that, we should be able to determine smart indent.


Actipro Software Support

Posted 19 years ago by tobias weltner
Avatar
Thanks!

Part of this may be resolved by introducing scopes in a sense that tokens need to be inside a valid parent scope. In this case, the "}" would really be two tokens, one on topmost scope, the other as part of a code block scope.

Scoping also helps prevent syntactically incorrect situations, for example it is illegal to have a function statement inside another, so if "function" would only be defined in the topmost scope but not in the "function" scope, it wouldn't be incorrectly identified as function token.

With this approach, you could keep this inside the language definition without the need for another parsing code. Also, indenting would be easy.

Just a suggestion. I worked with another editor control until recently, and there the outlined scopes worked great.
The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.