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); } }
static CSharpIndentEngine CreateEngine(string text) { var policy = FormattingOptionsFactory.CreateMono(); var sb = new StringBuilder(); int offset = 0; for (int i = 0; i < text.Length; i++) { var ch = text [i]; if (ch == '$') { offset = i; continue; } sb.Append(ch); } var document = new ReadOnlyDocument(sb.ToString()); var options = new TextEditorOptions(); var result = new CSharpIndentEngine(document, options, policy); result.UpdateToOffset(offset); return(result); }
protected override void CorrectIndentingImplementation (PolicyContainer policyParent, TextEditor editor, int line) { var lineSegment = editor.GetLine (line); if (lineSegment == null) return; try { var policy = policyParent.Get<CSharpFormattingPolicy> (MimeType); var textpolicy = policyParent.Get<TextStylePolicy> (MimeType); var tracker = new CSharpIndentEngine (policy.CreateOptions (textpolicy)); 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); } }
static CacheIndentEngine CreateIndentEngine(IDocument document, TextEditorOptions options) { // TODO Use project-specific formatter settings. But how to get a project from here? var formattingOptions = CSharpFormattingOptionsPersistence.GlobalOptions; var engine = new CSharpIndentEngine(document, options, formattingOptions.OptionsContainer.GetEffectiveOptions()); return(new CacheIndentEngine(engine)); }
public static IDocumentIndentEngine CreateEngine(string text, OptionSet formatOptions = null, IEnumerable <string> symbols = null) { var policy = formatOptions; if (policy == null) { policy = FormattingOptionsFactory.CreateMono(); // policy.IndentPreprocessorDirectives = false; // policy.AlignToFirstMethodCallArgument = policy.AlignToFirstIndexerArgument = true; } var sb = new StringBuilder(); int offset = 0; for (int i = 0; i < text.Length; i++) { var ch = text[i]; if (ch == '$') { offset = i; continue; } sb.Append(ch); } var document = SourceText.From(sb.ToString()); var csi = new CSharpIndentEngine(policy) { EnableCustomIndentLevels = true }; if (symbols != null) { foreach (var sym in symbols) { csi.DefineSymbol(sym); } } var result = new CacheIndentEngine(csi); result.Update(document, offset); return(result); }
void HandleTextOptionsChanged (object sender, EventArgs e) { var policy = Policy.CreateOptions (); var options = Editor.CreateNRefactoryTextEditorOptions (); options.IndentBlankLines = true; IStateMachineIndentEngine indentEngine; try { var csharpIndentEngine = new CSharpIndentEngine (textEditorData.Document, options, policy); //csharpIndentEngine.EnableCustomIndentLevels = true; foreach (var symbol in MonoDevelop.CSharp.Highlighting.CSharpSyntaxMode.GetDefinedSymbols (document.Project)) { csharpIndentEngine.DefineSymbol (symbol); } indentEngine = csharpIndentEngine; } catch (Exception ex) { LoggingService.LogError ("Error while creating the c# indentation engine", ex); indentEngine = new NullIStateMachineIndentEngine (textEditorData.Document); } stateTracker = new CacheIndentEngine (indentEngine); textEditorData.IndentationTracker = new IndentVirtualSpaceManager (textEditorData, stateTracker); if (textEditorData.Options.IndentStyle == IndentStyle.Auto || textEditorData.Options.IndentStyle == IndentStyle.None) { textEditorData.TextPasteHandler = null; } else { textEditorData.TextPasteHandler = new TextPasteIndentEngine (stateTracker, options, policy); } }
internal static int GetCurrentParameterIndex (ICompletionWidget widget, int offset, int memberStart) { int cursor = widget.CurrentCodeCompletionContext.TriggerOffset; int i = offset; if (i > cursor) return -1; if (i == cursor) return 1; // parameters are 1 based IEnumerable<string> types = MonoDevelop.Ide.DesktopService.GetMimeTypeInheritanceChain (CSharpFormatter.MimeType); CSharpIndentEngine engine = new CSharpIndentEngine (MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<CSharpFormattingPolicy> (types)); int index = memberStart + 1; int parentheses = 0; int bracket = 0; do { char c = widget.GetChar (i - 1); engine.Push (c); switch (c) { case '{': if (!engine.IsInsideOrdinaryCommentOrString) bracket++; break; case '}': if (!engine.IsInsideOrdinaryCommentOrString) bracket--; break; case '(': if (!engine.IsInsideOrdinaryCommentOrString) parentheses++; break; case ')': if (!engine.IsInsideOrdinaryCommentOrString) parentheses--; break; case ',': if (!engine.IsInsideOrdinaryCommentOrString && parentheses == 1 && bracket == 0) index++; break; } i++; } while (i <= cursor && parentheses >= 0); return parentheses != 1 || bracket > 0 ? -1 : index; }
internal static int GetCurrentParameterIndex(MonoDevelop.Ide.Gui.TextEditor editor, int offset, int memberStart) { int cursor = editor.CursorPosition; int i = offset; if (i > cursor) { return(-1); } if (i == cursor) { return(1); // parameters are 1 based } IEnumerable <string> types = MonoDevelop.Ide.DesktopService.GetMimeTypeInheritanceChain(CSharpFormatter.MimeType); CSharpIndentEngine engine = new CSharpIndentEngine(MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <CSharpFormattingPolicy> (types)); int index = memberStart + 1; int parentheses = 0; int bracket = 0; do { char c = editor.GetCharAt(i - 1); engine.Push(c); switch (c) { case '{': if (!engine.IsInsideOrdinaryCommentOrString) { bracket++; } break; case '}': if (!engine.IsInsideOrdinaryCommentOrString) { bracket--; } break; case '(': if (!engine.IsInsideOrdinaryCommentOrString) { parentheses++; } break; case ')': if (!engine.IsInsideOrdinaryCommentOrString) { parentheses--; } break; case ',': if (!engine.IsInsideOrdinaryCommentOrString && parentheses == 1 && bracket == 0) { index++; } break; } i++; } while (i <= cursor && parentheses >= 0); return(parentheses != 1 || bracket > 0 ? -1 : index); }
static CacheIndentEngine CreateIndentEngine(IDocument document, TextEditorOptions options) { var engine = new CSharpIndentEngine(document, options, FormattingOptionsFactory.CreateSharpDevelop()); return(new CacheIndentEngine(engine)); }