Beispiel #1
0
        protected override async void CorrectIndentingImplementation(PolicyContainer policyParent, TextEditor editor, int line)
        {
            var lineSegment = editor.GetLine(line);

            if (lineSegment == null)
            {
                return;
            }

            try {
                Microsoft.CodeAnalysis.Options.OptionSet options = null;

                foreach (var doc in IdeApp.Workbench.Documents)
                {
                    if (doc.Editor == editor)
                    {
                        options = await doc.AnalysisDocument?.GetOptionsAsync();

                        break;
                    }
                }

                if (options == null)
                {
                    var policy     = policyParent.Get <CSharpFormattingPolicy> (MimeType);
                    var textpolicy = policyParent.Get <TextStylePolicy> (MimeType);
                    options = policy.CreateOptions(textpolicy);
                }

                var tracker = new CSharpIndentEngine(options);

                tracker.Update(IdeApp.Workbench.ActiveDocument.Editor, lineSegment.Offset);
                for (int i = lineSegment.Offset; i < lineSegment.Offset + lineSegment.Length; i++)
                {
                    tracker.Push(editor.GetCharAt(i));
                }

                string curIndent = lineSegment.GetIndentation(editor);

                int nlwsp = curIndent.Length;
                if (!tracker.LineBeganInsideMultiLineComment || (nlwsp < lineSegment.LengthIncludingDelimiter && editor.GetCharAt(lineSegment.Offset + nlwsp) == '*'))
                {
                    // Possibly replace the indent
                    string newIndent = tracker.ThisLineIndent;
                    if (newIndent != curIndent)
                    {
                        editor.ReplaceText(lineSegment.Offset, nlwsp, newIndent);
                    }
                }
            } catch (Exception e) {
                LoggingService.LogError("Error while indenting", e);
            }
        }
Beispiel #2
0
        public override void CorrectIndenting(PolicyContainer policyParent, IEnumerable <string> mimeTypeChain,
                                              TextEditorData data, int line)
        {
            DocumentLine lineSegment = data.Document.GetLine(line);

            if (lineSegment == null)
            {
                return;
            }

            try {
                var policy  = policyParent.Get <CSharpFormattingPolicy> (mimeTypeChain);
                var tracker = new CSharpIndentEngine(data.Document, data.CreateNRefactoryTextEditorOptions(), policy.CreateOptions());

                tracker.Update(lineSegment.Offset);
                for (int i = lineSegment.Offset; i < lineSegment.Offset + lineSegment.Length; i++)
                {
                    tracker.Push(data.Document.GetCharAt(i));
                }

                string curIndent = lineSegment.GetIndentation(data.Document);

                int nlwsp = curIndent.Length;
                if (!tracker.LineBeganInsideMultiLineComment || (nlwsp < lineSegment.LengthIncludingDelimiter && data.Document.GetCharAt(lineSegment.Offset + nlwsp) == '*'))
                {
                    // Possibly replace the indent
                    string newIndent = tracker.ThisLineIndent;
                    if (newIndent != curIndent)
                    {
                        data.Replace(lineSegment.Offset, nlwsp, newIndent);
                    }
                }
            } catch (Exception e) {
                LoggingService.LogError("Error while indenting", e);
            }
        }