Preserving formatting when indenting

SyntaxEditor for Windows Forms Forum

Posted 12 years ago by Matt Whitfield
Version: 4.0.0262
Avatar
Hi

When I have some text - for example

SELECT SiteCode
  FROM tblCfgSites
 WHERE ID > 5
 GROUP BY SiteCode
When I highlight this text and press tab - it indents all lines to the nearest tab width. So if the tab width was 4, you end up with

    SELECT SiteCode
    FROM tblCfgSites
    WHERE ID > 5
    GROUP BY SiteCode
Is there any way to make it so that it indents the text in a more simple manner (i.e. just adds a tab to the start of each line)? Because I'd like it to preserve formatting like the above, ideally.

Comments (10)

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Matt,

Good point, the indent code isn't implemented like that right now. We'll add this sort of thing to the TODO list. But if you need this functionality right now you can write your own EditCommand and swap our IndentCommand out for it in the SyntaxEditor.CommandLinks collection.


Actipro Software Support

Posted 12 years ago by Matt Whitfield
Avatar
Ok thanks

Is that likely to arrive soon (i.e. maybe within next 5 maintenance releases?). If not then i'll probably go ahead and write the EditCommand.

But - i have absolutely no idea how to do that. I don't have a problem with RTFM - but I do have a problem in knowing which bit to read, I find it very hard to navigate... Could you possible provide me with some pointers in the right direction? Ideal situation would be if you could post the edit command that is used for indent at the moment, or just a noddy example (such as 'delete text')

Thanks
Posted 12 years ago by Matt Whitfield
Avatar
Actually

I did a bit more RTFMing, and I managed it using the following code - please let me know if it's entirely crap!

The edit commands:

    public class StrictTabIndentCommand : EditCommand
    {
        public override void Execute(EditCommandContext context)
        {
            if (context.Document.AutoConvertTabsToSpaces)
            {
                string s = new string(' ', context.Document.TabSize);
                context.View.CommentLines(s);
            }
            else
            {
                context.View.CommentLines("\t");
            }
        }
    }
    public class StrictTabOutdentCommand : EditCommand
    {
        public override void Execute(EditCommandContext context)
        {
            if (context.Document.AutoConvertTabsToSpaces)
            {
                string s = new string(' ', context.Document.TabSize);
                context.View.UncommentLines(s);
            }
            else
            {
                context.View.UncommentLines("\t");
            }
        }
    }
The CommandLinks switch:

                foreach (CommandLink cl in shtb.CommandLinks)
                {
                    if (cl.Command.Name == "IndentCommand")
                    {
                        cl.Command = new StrictTabIndentCommand();
                    }
                    if (cl.Command.Name == "OutdentCommand")
                    {
                        cl.Command = new StrictTabOutdentCommand();
                    }
                }
Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
No that should work. I like your creative use of the comment/uncomment lines feature. :)

We'll try and get to updating it ourselves but are not sure of a timeframe right now.


Actipro Software Support

Posted 12 years ago by Matt Whitfield
Avatar
Brilliant, thanks! :)
Posted 12 years ago by Matt Whitfield
Avatar
Hmmm sorry I have another question on this.

I was under the impression that IndentCommand and OutdentCommand would only be fired when tab was pressed with a selection - but it appears that they handle normal tabbing too...

Can someone give me some quick pointers on how i need to structure it so that when there is no selection the normal indentcommand and outdentcommand are called?

Do i need to call them from my edit command? Is that even possible?
Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Yes, maybe have your indent commands inherit ours and you could check "context.Selection.EditPositionRange.Lines = 1". If true, call the base Execute method.


Actipro Software Support

Posted 12 years ago by Matt Whitfield
Avatar
Ok - have tried that - still doesn't seem to work.

here is my edit command

    public class StrictTabIndentCommand : IndentCommand
    {
        public override void Execute(EditCommandContext context)
        {
            if (context.View.SelectedText.Length == 0)
            {
                base.Execute(context);
            }
            else
            {
                if (context.Document.AutoConvertTabsToSpaces)
                {
                    string s = new string(' ', context.Document.TabSize);
                    context.View.CommentLines(s);
                }
                else
                {
                    context.View.CommentLines("\t");
                }
            }
        }
    }
and here is the code that replaces the command link

                foreach (CommandLink cl in shtb.CommandLinks)
                {
                    if (cl.Command.Name == "IndentCommand")
                    {
                        cl.Command = new StrictTabIndentCommand();
                    }
                }
Do you need me to reproduce this in the sample app to take a look at it? Becuase it's very weird - what happens when I call base.Execute() is that it ends up still creating a selection if there was none, and indents the line rather than adding a tab character. Is there another way I can cause the tab character to be inserted (and removed for the strict outdent command)?
Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Oh sorry I should have mentioned call the base constructor with a parameter of true like this:
public StrictTabIndentCommand() : base(true) {
That tells it to mimic typing behavior; otherwise it will always do the non-tab inserting behavior.


Actipro Software Support

Posted 12 years ago by Matt Whitfield
Avatar
That's the puppy - thanks!
The latest build of this product (v2018.1 build 0341) was released 9 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.