public override void Initialize()
        {
            base.Initialize();


            if (textEditorData != null)
            {
                textEditorData.Options.Changed += HandleTextOptionsChanged;

                var policy  = Policy.CreateOptions();
                var options = Editor.CreateNRefactoryTextEditorOptions();
                options.IndentBlankLines = true;
                var engine = new CacheIndentEngine(new ICSharpCode.NRefactory.CSharp.CSharpIndentEngine(textEditorData.Document, options, policy));
                textEditorData.IndentationTracker = new IndentVirtualSpaceManager(
                    textEditorData,
                    engine
                    );

                textEditorData.Document.TextReplacing += HandleTextReplacing;
                textEditorData.Document.TextReplaced  += HandleTextReplaced;

                textEditorData.TextPasteHandler = new TextPasteIndentEngine(engine.Clone(), options);
                textEditorData.Paste           += HandleTextPaste;
            }

            InitTracker();
        }
Beispiel #2
0
        void DoReSmartIndent(int cursor)
        {
            SafeUpdateIndentEngine(cursor);
            if (stateTracker.LineBeganInsideVerbatimString || stateTracker.LineBeganInsideMultiLineComment)
            {
                return;
            }
            var line = Editor.GetLineByOffset(cursor);

            // Get context to the end of the line w/o changing the main engine's state
            var curTracker = stateTracker.Clone();

            try {
                for (int max = cursor; max < line.EndOffset; max++)
                {
                    curTracker.Push(Editor.GetCharAt(max));
                }
            } catch (Exception e) {
                LoggingService.LogError("Exception during indentation", e);
            }

            int    pos       = line.Offset;
            string curIndent = line.GetIndentation(Editor);
            int    nlwsp     = curIndent.Length;
            int    offset    = cursor > pos + nlwsp ? cursor - (pos + nlwsp) : 0;

            if (!stateTracker.LineBeganInsideMultiLineComment || (nlwsp < line.LengthIncludingDelimiter && Editor.GetCharAt(line.Offset + nlwsp) == '*'))
            {
                // Possibly replace the indent
                string newIndent       = curTracker.ThisLineIndent;
                int    newIndentLength = newIndent.Length;
                if (newIndent != curIndent)
                {
                    if (CompletionWindowManager.IsVisible)
                    {
                        if (pos < CompletionWindowManager.CodeCompletionContext.TriggerOffset)
                        {
                            CompletionWindowManager.CodeCompletionContext.TriggerOffset -= nlwsp;
                        }
                    }

                    Editor.ReplaceText(pos, nlwsp, newIndent);
                    newIndentLength = newIndent.Length;
                    CompletionWindowManager.HideWindow();
                }
                pos += newIndentLength;
            }
            else
            {
                pos += curIndent.Length;
            }

            pos += offset;

            Editor.FixVirtualIndentation();
        }