public static CacheIndentEngine CreateEngine(string text, CSharpFormattingOptions formatOptions = null, TextEditorOptions options = null) { if (formatOptions == null) { formatOptions = FormattingOptionsFactory.CreateMono(); formatOptions.AlignToFirstIndexerArgument = formatOptions.AlignToFirstMethodCallArgument = 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 = new ReadOnlyDocument(sb.ToString()); options = options ?? new TextEditorOptions { EolMarker = "\n" }; var result = new CacheIndentEngine(new CSharpIndentEngine(document, options, formatOptions)); result.Update(offset); return result; }
public OmniSharpConfiguration() { PathReplacements = new List<PathReplacement>(); IgnoredCodeIssues = new List<string>(); TextEditorOptions = new TextEditorOptions (); CSharpFormattingOptions = FormattingOptionsFactory.CreateAllman(); }
/// <summary> /// Initializes a new instance of the <see cref="ICSharpCode.NRefactory.CSharp.CSharpFormatter"/> class. /// </summary> /// <param name="policy">The formatting policy to use.</param> /// <param name="document">The text document to work upon.</param> /// <param name="options">The text editor options (optional). Default is: TextEditorOptions.Default</param> public CSharpFormatter(CSharpFormattingOptions policy, TextEditorOptions options = null) { if (policy == null) throw new ArgumentNullException("policy"); this.policy = policy; this.options = options ?? TextEditorOptions.Default; }
public static void RandomTests(string filePath, int count, CSharpFormattingOptions policy = null, TextEditorOptions options = null) { if (File.Exists(filePath)) { var code = File.ReadAllText(filePath); var document = new ReadOnlyDocument(code); policy = policy ?? FormattingOptionsFactory.CreateMono(); options = options ?? new TextEditorOptions { IndentBlankLines = false }; var engine = new CacheIndentEngine(new CSharpIndentEngine(document, options, policy) { EnableCustomIndentLevels = true }); Random rnd = new Random(); for (int i = 0; i < count; i++) { int offset = rnd.Next(document.TextLength); engine.Update(offset); if (engine.CurrentIndent.Length == 0) continue; } } else { Assert.Fail("File " + filePath + " doesn't exist."); } }
protected static IDocument Test (CSharpFormattingOptions policy, string input, string expectedOutput, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { expectedOutput = NormalizeNewlines(expectedOutput); IDocument doc = GetResult(policy, input, mode, options); if (expectedOutput != doc.Text) { Console.WriteLine ("expected:"); Console.WriteLine (expectedOutput); Console.WriteLine ("got:"); Console.WriteLine (doc.Text); for (int i = 0; i < expectedOutput.Length && i < doc.TextLength; i++) { if (expectedOutput [i] != doc.GetCharAt(i)) { Console.WriteLine ( "i:"+i+" differ:"+ expectedOutput[i].ToString ().Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t") + " !=" + doc.GetCharAt(i).ToString ().Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t") ); Console.WriteLine(">"+expectedOutput.Substring (i).Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t")); Console.WriteLine(">"+doc.Text.Substring (i).Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t")); break; } } } Assert.AreEqual (expectedOutput, doc.Text); return doc; }
Indent(Indent engine) { this.indentStack = engine.indentStack.Clone(); this.options = engine.options; this.curIndent = engine.curIndent; this.extraSpaces = engine.extraSpaces; this.indentString = engine.indentString; }
/// <summary> /// Creates a new TextPasteIndentEngine instance. /// </summary> /// <param name="decoratedEngine"> /// An instance of <see cref="IStateMachineIndentEngine"/> to which the /// logic for indentation will be delegated. /// </param> /// <param name="textEditorOptions"> /// Text editor options for indentation. /// </param> /// <param name="formattingOptions"> /// C# formatting options. /// </param> public TextPasteIndentEngine(IStateMachineIndentEngine decoratedEngine, TextEditorOptions textEditorOptions, CSharpFormattingOptions formattingOptions) { this.engine = decoratedEngine; this.textEditorOptions = textEditorOptions; this.formattingOptions = formattingOptions; this.engine.EnableCustomIndentLevels = false; }
Indent(Indent engine) { this.indentStack = new Stack<IndentType>(engine.indentStack.Reverse()); this.options = engine.options; this.curIndent = engine.curIndent; this.extraSpaces = engine.extraSpaces; this.indentString = engine.indentString; }
protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { StringBuilderDocument document; var changes = GetChanges(policy, input, out document, mode, options); changes.ApplyChanges(); return document; }
public CSharpIndentEngine(IDocument document, TextEditorOptions textEditorOptions, CSharpFormattingOptions formattingOptions) { this.document = document; this.options = formattingOptions; this.textEditorOptions = textEditorOptions; this.indent = new Indent(textEditorOptions); this.thisLineindent = new Indent(textEditorOptions); }
/// <summary> /// Creates a new TextPasteIndentEngine instance. /// </summary> /// <param name="decoratedEngine"> /// An instance of <see cref="IStateMachineIndentEngine"/> to which the /// logic for indentation will be delegated. /// </param> /// <param name="textEditorOptions"> /// Text editor options for indentation. /// </param> /// <param name="formattingOptions"> /// C# formatting options. /// </param> public TextPasteIndentEngine(IStateMachineIndentEngine decoratedEngine, TextEditorOptions textEditorOptions, CSharpFormattingOptions formattingOptions) { this.engine = decoratedEngine; this.textEditorOptions = textEditorOptions; this.formattingOptions = formattingOptions; this.engine.EnableCustomIndentLevels = false; }
/*public static string ApplyChanges (string text, List<TextReplaceAction> changes) { changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset)); StringBuilder b = new StringBuilder(text); foreach (var change in changes) { //Console.WriteLine ("---- apply:" + change); // Console.WriteLine (adapter.Text); if (change.Offset > b.Length) continue; b.Remove(change.Offset, change.RemovedChars); b.Insert(change.Offset, change.InsertedText); } // Console.WriteLine ("---result:"); // Console.WriteLine (adapter.Text); return b.ToString(); }*/ static TextEditorOptions GetActualOptions(TextEditorOptions options) { if (options == null) { options = new TextEditorOptions(); options.EolMarker = "\n"; options.WrapLineLength = 80; } return options; }
public CSharpIndentEngine(IDocument document, TextEditorOptions textEditorOptions, CSharpFormattingOptions formattingOptions) { this.document = document; this.options = formattingOptions; this.textEditorOptions = textEditorOptions; this.indent = new Indent(textEditorOptions); this.indentDelta = new Indent(textEditorOptions); this.thisLineindent = new Indent(textEditorOptions); }
/// <summary> /// Initializes a new instance of the <see cref="ICSharpCode.NRefactory.CSharp.CSharpFormatter"/> class. /// </summary> /// <param name="policy">The formatting policy to use.</param> /// <param name="options">The text editor options (optional). Default is: TextEditorOptions.Default</param> public CSharpFormatter(CSharpFormattingOptions policy, TextEditorOptions options = null) { if (policy == null) { throw new ArgumentNullException("policy"); } this.policy = policy; this.options = options ?? TextEditorOptions.Default; }
static CacheIndentEngine CreateIndentEngine(IDocument document, TextEditorOptions options) { IProject currentProject = null; var projectService = SD.GetService<IProjectService>(); if (projectService != null) { currentProject = projectService.FindProjectContainingFile(new FileName(document.FileName)); } var formattingOptions = CSharpFormattingOptionsPersistence.GetProjectOptions(currentProject); var engine = new CSharpIndentEngine(document, options, formattingOptions.OptionsContainer.GetEffectiveOptions()); return new CacheIndentEngine(engine); }
protected static FormattingChanges GetChanges(CSharpFormattingOptions policy, string input, out StringBuilderDocument document, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { options = GetActualOptions(options); input = NormalizeNewlines(input); document = new StringBuilderDocument(input); var visitor = new CSharpFormatter(policy, options); visitor.FormattingMode = mode; var syntaxTree = new CSharpParser().Parse(document, "test.cs"); return visitor.AnalyzeFormatting(document, syntaxTree); }
/*public static string ApplyChanges (string text, List<TextReplaceAction> changes) { changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset)); StringBuilder b = new StringBuilder(text); foreach (var change in changes) { //Console.WriteLine ("---- apply:" + change); // Console.WriteLine (adapter.Text); if (change.Offset > b.Length) continue; b.Remove(change.Offset, change.RemovedChars); b.Insert(change.Offset, change.InsertedText); } // Console.WriteLine ("---result:"); // Console.WriteLine (adapter.Text); return b.ToString(); }*/ protected static IDocument GetResult (CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.OnTheFly) { input = NormalizeNewlines (input); var document = new StringBuilderDocument (input); var options = new TextEditorOptions (); options.EolMarker = "\n"; var visitor = new AstFormattingVisitor (policy, document, options); visitor.FormattingMode = mode; var compilationUnit = new CSharpParser ().Parse (document, "test.cs"); compilationUnit.AcceptVisitor (visitor); visitor.ApplyChanges(); return document; }
public AstFormattingVisitor(CSharpFormattingOptions policy, IDocument document, TextEditorOptions options = null) { if (policy == null) { throw new ArgumentNullException("policy"); } if (document == null) { throw new ArgumentNullException("document"); } this.policy = policy; this.document = document; this.options = options ?? TextEditorOptions.Default; curIndent = new Indent(this.options); }
protected static void Continue(CSharpFormattingOptions policy, IDocument document, string expectedOutput, FormattingMode formattingMode = FormattingMode.OnTheFly) { expectedOutput = NormalizeNewlines (expectedOutput); var options = new TextEditorOptions (); options.EolMarker = "\n"; var formatter = new CSharpFormatter (policy, options); formatter.FormattingMode = formattingMode; string newText = formatter.Format (document); if (expectedOutput != newText) { Console.WriteLine (newText); } Assert.AreEqual (expectedOutput, newText); }
/// <summary> /// Creates a new CSharpIndentEngine instance. /// </summary> /// <param name="document"> /// An instance of <see cref="IDocument"/> which is being parsed. /// </param> /// <param name="formattingOptions"> /// C# formatting options. /// </param> /// <param name="textEditorOptions"> /// Text editor options for indentation. /// </param> public CSharpIndentEngine(IDocument document, TextEditorOptions textEditorOptions, CSharpFormattingOptions formattingOptions) { this.formattingOptions = formattingOptions; this.textEditorOptions = textEditorOptions; this.document = document; this.currentState = new GlobalBodyState(this); this.conditionalSymbols = new HashSet <string>(); this.customConditionalSymbols = new HashSet <string>(); this.wordToken = new StringBuilder(); this.previousKeyword = string.Empty; this.newLineChar = textEditorOptions.EolMarker[0]; }
/*public static string ApplyChanges (string text, List<TextReplaceAction> changes) { changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset)); StringBuilder b = new StringBuilder(text); foreach (var change in changes) { //Console.WriteLine ("---- apply:" + change); // Console.WriteLine (adapter.Text); if (change.Offset > b.Length) continue; b.Remove(change.Offset, change.RemovedChars); b.Insert(change.Offset, change.InsertedText); } // Console.WriteLine ("---result:"); // Console.WriteLine (adapter.Text); return b.ToString(); }*/ protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive) { input = NormalizeNewlines(input); var document = new StringBuilderDocument(input); var options = new TextEditorOptions(); options.EolMarker = "\n"; options.WrapLineLength = 80; var visitor = new CSharpFormatter (policy, options); visitor.FormattingMode = mode; var syntaxTree = new CSharpParser ().Parse (document, "test.cs"); var changes = visitor.AnalyzeFormatting(document, syntaxTree); changes.ApplyChanges(); return document; }
public CodeFormatResponse Format(CodeFormatRequest request) { var document = new StringBuilderDocument(request.Buffer); var options = new TextEditorOptions(); options.EolMarker = Environment.NewLine; options.WrapLineLength = 80; options.TabsToSpaces = request.ExpandTab; var policy = FormattingOptionsFactory.CreateAllman(); var visitor = new AstFormattingVisitor(policy, document, options); visitor.FormattingMode = FormattingMode.Intrusive; var syntaxTree = new CSharpParser().Parse(document, request.FileName); syntaxTree.AcceptVisitor(visitor); visitor.ApplyChanges(); return new CodeFormatResponse(document.Text); }
protected static void Continue (CSharpFormattingOptions policy, IDocument document, string expectedOutput, FormattingMode formattingMode = FormattingMode.OnTheFly) { expectedOutput = NormalizeNewlines (expectedOutput); var options = new TextEditorOptions (); options.EolMarker = "\n"; var visitior = new AstFormattingVisitor (policy, document, options); visitior.FormattingMode = formattingMode; var syntaxTree = new CSharpParser ().Parse (document, "test.cs"); syntaxTree.AcceptVisitor (visitior); visitior.ApplyChanges(); string newText = document.Text; if (expectedOutput != newText) { Console.WriteLine (newText); } Assert.AreEqual (expectedOutput, newText); }
public IndentEngine(IndentEngine prototype) { this.Document = prototype.Document; this.Options = prototype.Options; this.TextEditorOptions = prototype.TextEditorOptions; this.CurrentState = prototype.CurrentState.Clone(); this.ConditionalSymbols = new HashSet <string>(prototype.ConditionalSymbols); this.IfDirectiveEvalResult = prototype.IfDirectiveEvalResult; this.CurrentIndent = new StringBuilder(prototype.CurrentIndent.ToString()); this.offset = prototype.offset; this.line = prototype.line; this.column = prototype.column; this.IsLineStart = prototype.IsLineStart; this.CurrentChar = prototype.CurrentChar; this.PreviousChar = prototype.PreviousChar; }
CSharpIndentEngine(CSharpIndentEngine prototype) { this.document = prototype.document; this.options = prototype.options; this.textEditorOptions = prototype.textEditorOptions; this.indent = prototype.indent.Clone(); this.thisLineindent = prototype.thisLineindent.Clone(); this.offset = prototype.offset; this.inside = prototype.inside; this.IsLineStart = prototype.IsLineStart; this.pc = prototype.pc; this.parenStack = new Stack<TextLocation>(prototype.parenStack.Reverse ()); this.currentBody = prototype.currentBody; this.nextBody = prototype.nextBody; this.addContinuation = prototype.addContinuation; this.line = prototype.line; this.col = prototype.col; this.popNextParenBlock = prototype.popNextParenBlock; }
CSharpIndentEngine(CSharpIndentEngine prototype) { this.document = prototype.document; this.options = prototype.options; this.textEditorOptions = prototype.textEditorOptions; this.indent = prototype.indent.Clone(); this.indentDelta = prototype.indentDelta.Clone(); this.thisLineindent = prototype.thisLineindent.Clone(); this.offset = prototype.offset; this.inside = prototype.inside; this.IsLineStart = prototype.IsLineStart; this.pc = prototype.pc; this.parenStack = new Stack <TextLocation>(prototype.parenStack.Reverse()); this.currentBody = prototype.currentBody; this.nextBody = prototype.nextBody; this.addContinuation = prototype.addContinuation; this.line = prototype.line; this.col = prototype.col; this.popNextParenBlock = prototype.popNextParenBlock; }
public static IDocumentIndentEngine CreateEngine(string text, CSharpFormattingOptions 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 = new ReadOnlyDocument(sb.ToString()); var options = new TextEditorOptions(); var csi = new CSharpIndentEngine(document, options, policy) { EnableCustomIndentLevels = true }; if (symbols != null) { foreach (var sym in symbols) { csi.DefineSymbol(sym); } } var result = new CacheIndentEngine(csi); result.Update(offset); return result; }
public Indent(TextEditorOptions options) { this.options = options; }
/// <summary> /// Creates a new CSharpIndentEngine instance. /// </summary> /// <param name="document"> /// An instance of <see cref="IDocument"/> which is being parsed. /// </param> /// <param name="formattingOptions"> /// C# formatting options. /// </param> /// <param name="textEditorOptions"> /// Text editor options for indentation. /// </param> public CSharpIndentEngine(IDocument document, TextEditorOptions textEditorOptions, CSharpFormattingOptions formattingOptions) { this.formattingOptions = formattingOptions; this.textEditorOptions = textEditorOptions; this.document = document; this.currentState = new GlobalBodyState(this); this.conditionalSymbols = new HashSet<string>(); this.customConditionalSymbols = new HashSet<string>(); this.wordToken = new StringBuilder(); this.previousKeyword = string.Empty; this.newLineChar = textEditorOptions.EolMarker[0]; }
/// <summary> /// Creates a new CSharpIndentEngine instance from the given prototype. /// </summary> /// <param name="prototype"> /// An CSharpIndentEngine instance. /// </param> public CSharpIndentEngine(CSharpIndentEngine prototype) { this.formattingOptions = prototype.formattingOptions; this.textEditorOptions = prototype.textEditorOptions; this.document = prototype.document; this.newLineChar = prototype.newLineChar; this.currentState = prototype.currentState.Clone(this); this.conditionalSymbols = new HashSet<string>(prototype.conditionalSymbols); this.customConditionalSymbols = new HashSet<string>(prototype.customConditionalSymbols); this.wordToken = new StringBuilder(prototype.wordToken.ToString()); this.previousKeyword = string.Copy(prototype.previousKeyword); this.offset = prototype.offset; this.line = prototype.line; this.column = prototype.column; this.isLineStart = prototype.isLineStart; this.isLineStartBeforeWordToken = prototype.isLineStartBeforeWordToken; this.currentChar = prototype.currentChar; this.previousChar = prototype.previousChar; this.previousNewline = prototype.previousNewline; this.currentIndent = new StringBuilder(prototype.CurrentIndent.ToString()); this.lineBeganInsideMultiLineComment = prototype.lineBeganInsideMultiLineComment; this.lineBeganInsideVerbatimString = prototype.lineBeganInsideVerbatimString; this.ifDirectiveEvalResults = prototype.ifDirectiveEvalResults.Clone(); this.ifDirectiveIndents = prototype.ifDirectiveIndents.Clone(); this.EnableCustomIndentLevels = prototype.EnableCustomIndentLevels; }
public ConstructFixer(CSharpFormattingOptions options, TextEditorOptions textEditorOptions) { this.options = options; this.textEditorOptions = textEditorOptions; }
static TextEditorOptions CreateInvariantOptions() { var options = new TextEditorOptions(); options.EolMarker = "\n"; return options; }
protected static void TestNoUnnecessaryChanges(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { var formatted = GetResult(policy, input, mode, options).Text; var changes = GetChanges(policy, formatted, mode, options); Assert.AreEqual(0, changes.Count, "Wrong amount of changes"); }
public static void ReadAndTest(string filePath, CSharpFormattingOptions policy = null, TextEditorOptions options = null) { if (File.Exists(filePath)) { filePath = Path.GetFullPath(filePath); var code = File.ReadAllText(filePath); var document = new ReadOnlyDocument(code); if (policy == null) { policy = FormattingOptionsFactory.CreateMono(); policy.AlignToFirstIndexerArgument = policy.AlignToFirstMethodCallArgument = true; } options = options ?? new TextEditorOptions { IndentBlankLines = false }; var engine = new CacheIndentEngine(new CSharpIndentEngine(document, options, policy) { EnableCustomIndentLevels = true }); int errors = 0; foreach (var ch in code) { if (options.EolMarker[0] == ch) { if (!(engine.LineBeganInsideMultiLineComment || engine.LineBeganInsideVerbatimString)) { if (engine.CurrentIndent.Length > 0) { if (engine.NeedsReindent) { errors++; Console.WriteLine(string.Format("Indent: {2}, Current indent: {3} in {0}:{1}", filePath, engine.Location.Line, engine.ThisLineIndent.Length, engine.CurrentIndent.Length)); } } } } engine.Push(ch); } Assert.AreEqual(0, errors); } else { Assert.Fail("File " + filePath + " doesn't exist."); } }
protected static FormattingChanges GetChanges(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null) { StringBuilderDocument document; return GetChanges(policy, input, out document, mode, options); }
public Indent(TextEditorOptions options) { this.options = options; Reset(); }
Indent(TextEditorOptions options, Stack <IndentType> indentStack, int curIndent) : this(options) { this.indentStack = indentStack; this.curIndent = curIndent; }
public ConstructFixer(CSharpFormattingOptions options, TextEditorOptions textEditorOptions) { this.options = options; this.textEditorOptions = textEditorOptions; }
public void CustomizeEditorOptions(TextEditorOptions editorOptions) { int? indentationSize = GetEffectiveIndentationSize(); if (indentationSize.HasValue) { editorOptions.IndentSize = indentationSize.Value; editorOptions.TabSize = indentationSize.Value; editorOptions.ContinuationIndent = indentationSize.Value; } bool? convertTabsToSpaces = GetEffectiveConvertTabsToSpaces(); if (convertTabsToSpaces.HasValue) editorOptions.TabsToSpaces = convertTabsToSpaces.Value; }