protected PropertySyntax CreateApplicationAreaProperty(SyntaxNode node) { //calculate indent int indentLength = 4; string indent; SyntaxTriviaList leadingTrivia = node.GetLeadingTrivia(); if (leadingTrivia != null) { indent = leadingTrivia.ToString(); int newLinePos = indent.LastIndexOf("/n"); if (newLinePos >= 0) { indent = indent.Substring(newLinePos + 1); } indentLength += indent.Length; } indent = "".PadLeft(indentLength); SyntaxTriviaList leadingTriviaList = SyntaxFactory.ParseLeadingTrivia(indent, 0); SyntaxTriviaList trailingTriviaList = SyntaxFactory.ParseTrailingTrivia("\r\n", 0); SeparatedSyntaxList <IdentifierNameSyntax> values = new SeparatedSyntaxList <IdentifierNameSyntax>(); values = values.Add(SyntaxFactory.IdentifierName(this.ApplicationAreaName)); return(SyntaxFactory.Property(PropertyKind.ApplicationArea, SyntaxFactory.CommaSeparatedPropertyValue(values)) .WithLeadingTrivia(leadingTriviaList) .WithTrailingTrivia(trailingTriviaList)); }
public static Task <Document> AddNewLineBeforeInsteadOfAfterAsync( Document document, SyntaxNodeOrToken left, SyntaxNodeOrToken middle, SyntaxNodeOrToken right, CancellationToken cancellationToken = default) { StringBuilder sb = StringBuilderCache.GetInstance(); SyntaxTriviaList trailingTrivia = middle.GetTrailingTrivia(); if (IsOptionalWhitespaceThenEndOfLineTrivia(trailingTrivia)) { sb.Append(DetermineEndOfLine(middle).ToString()); } else { sb.Append(trailingTrivia.ToString()); } sb.Append(right.GetLeadingTrivia().ToString()); sb.Append(middle.ToString()); sb.Append(" "); return(document.WithTextChangeAsync( TextSpan.FromBounds(left.Span.End, right.SpanStart), StringBuilderCache.GetStringAndFree(sb), cancellationToken)); }
public static Task <Document> AddNewLineAfterInsteadOfBeforeAsync( Document document, SyntaxNodeOrToken left, SyntaxNodeOrToken middle, SyntaxNodeOrToken right, CancellationToken cancellationToken = default) { StringBuilder sb = StringBuilderCache.GetInstance(); sb.Append(" "); sb.Append(middle.ToString()); SyntaxTriviaList trailingTrivia = left.GetTrailingTrivia(); if (SyntaxTriviaAnalysis.IsOptionalWhitespaceThenEndOfLineTrivia(trailingTrivia)) { sb.Append(SyntaxTriviaAnalysis.GetEndOfLine(left).ToString()); } else { sb.Append(trailingTrivia.ToString()); } sb.Append(middle.GetLeadingTrivia().ToString()); string newText = StringBuilderCache.GetStringAndFree(sb); var textChange = new TextChange( TextSpan.FromBounds(left.Span.End, right.SpanStart), newText); return(document.WithTextChangeAsync(textChange, cancellationToken)); }
/* * The method below processes a list of trivia. */ private void ProcessTrivia(SyntaxTriviaList trivia) { for (var i = 0; i < trivia.Count; i++) { switch (trivia[i].Kind()) { /* * If the trivia contains multi-line comment, we will output it as * documentation. Before doing that, we need to strip the comment * delimiters from it. */ case SyntaxKind.MultiLineCommentTrivia: AppendMarkdown(StripCommentDelimiters(trivia.ToString())); break; /* * Whitespace and eol markers are skipped if they preceed or follow * trivia that we don't want to output. The `IsSkippedTrivia` function * decides whether a piece of trivia is outputted or not. */ case SyntaxKind.WhitespaceTrivia: if (i == trivia.Count - 1 || !IsSkippedTrivia(trivia[i + 1].Kind())) { OutputTrivia(trivia[i]); } break; case SyntaxKind.EndOfLineTrivia: if (i == 0 || !IsSkippedTrivia(trivia[i - 1].Kind())) { OutputTrivia(trivia[i]); } break; /* * When we encounter either `#region` or `#endregion` directive, we * start a new block. The `VisitRegionDirectiveTrivia` visitor method * actually creates the new macro. Note that we omit the directives * in the output altogether. */ case SyntaxKind.RegionDirectiveTrivia: case SyntaxKind.EndRegionDirectiveTrivia: _startNewBlock = true; break; /* * In case of any other trivia we revert to the generic method which * outputs it verbatim as code. */ default: OutputTrivia(trivia[i]); break; } } }
private static NamespaceDeclarationSyntax FormatLeadingWhitespace(NamespaceDeclarationSyntax namespaceNode) { SyntaxTriviaList leadingTrivia = namespaceNode.GetLeadingTrivia(); string trimmedLeadingTrivia = leadingTrivia.ToString().TrimEnd(); SyntaxTrivia desiredNewlines = GetNewlinesForNamespaceLeadingTrivia(namespaceNode); SyntaxTrivia newLeadingTrivia = SyntaxFactory.SyntaxTrivia(SyntaxKind.SingleLineCommentTrivia, trimmedLeadingTrivia); SyntaxTriviaList updatedLeadingTrivia = SyntaxFactory.TriviaList(newLeadingTrivia, desiredNewlines); return(namespaceNode.WithLeadingTrivia(updatedLeadingTrivia)); }
protected PropertySyntax CreateToolTipProperty(SyntaxNode node) { //calculate indent int indentLength = 4; string indent; SyntaxTriviaList leadingTrivia = node.GetLeadingTrivia(); if (leadingTrivia != null) { indent = leadingTrivia.ToString(); int newLinePos = indent.LastIndexOf("/n"); if (newLinePos >= 0) { indent = indent.Substring(newLinePos + 1); } indentLength += indent.Length; } indent = "".PadLeft(indentLength); SyntaxTriviaList leadingTriviaList = SyntaxFactory.ParseLeadingTrivia(indent, 0); SyntaxTriviaList trailingTriviaList = SyntaxFactory.ParseTrailingTrivia("\r\n", 0); string ToolTipValue = ""; string ObjName = node.GetProperty("Caption") != null?node.GetProperty("Caption").Value.ToString() : node.GetNameStringValue(); ObjName = ObjName.Replace("'", ""); if (node.Kind == SyntaxKind.PageAction) { ToolTipValue = PageActionTooltip; if (ToolTipValue.Contains("%1")) { ToolTipValue = ToolTipValue.Replace("%1", ObjName); } } else if (node.Kind == SyntaxKind.PageField) { ToolTipValue = PageFieldTooltip; if (ToolTipValue.Contains("%1")) { ToolTipValue = ToolTipValue.Replace("%1", ObjName); } } return(SyntaxFactory.PropertyLiteral(PropertyKind.ToolTip, ToolTipValue) .WithLeadingTrivia(leadingTriviaList) .WithTrailingTrivia(trailingTriviaList)); }
protected PropertySyntax CreateApplicationAreaProperty(SyntaxNode node) { //calculate indent int indentLength = 4; string indent; SyntaxTriviaList leadingTrivia = node.GetLeadingTrivia(); if (leadingTrivia != null) { indent = leadingTrivia.ToString(); int newLinePos = indent.LastIndexOf("/n"); if (newLinePos >= 0) { indent = indent.Substring(newLinePos + 1); } indentLength += indent.Length; } indent = "".PadLeft(indentLength); SyntaxTriviaList leadingTriviaList = SyntaxFactory.ParseLeadingTrivia(indent, 0); SyntaxTriviaList trailingTriviaList = SyntaxFactory.ParseTrailingTrivia("\r\n", 0); SeparatedSyntaxList <IdentifierNameSyntax> values = new SeparatedSyntaxList <IdentifierNameSyntax>(); values = values.Add(SyntaxFactory.IdentifierName(this.ApplicationAreaName)); //try to convert from string to avoid issues with enum ids changed between AL compiler versions PropertyKind propertyKind; try { propertyKind = (PropertyKind)Enum.Parse(typeof(PropertyKind), "ApplicationArea", true); } catch (Exception) { propertyKind = PropertyKind.ApplicationArea; } return(SyntaxFactory.Property(propertyKind, SyntaxFactory.CommaSeparatedPropertyValue(values)) .WithLeadingTrivia(leadingTriviaList) .WithTrailingTrivia(trailingTriviaList)); }
private static SyntaxTrivia GetNewArrowLeadingTrivia(SyntaxNode node) { SyntaxTriviaList leadingTrivia = node.GetLeadingTrivia(); string leadingTriviaString = leadingTrivia.ToString(); int indexOfLastNewline = leadingTriviaString.LastIndexOf(Environment.NewLine); int lengthOfLastLineleadingTrivia = leadingTriviaString.Length; if (indexOfLastNewline != -1) { lengthOfLastLineleadingTrivia = lengthOfLastLineleadingTrivia - indexOfLastNewline - Environment.NewLine.Length; } if (lengthOfLastLineleadingTrivia <= 0) { return(SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, IndentSpacing)); } return(SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, GetSpaceString(lengthOfLastLineleadingTrivia) + IndentSpacing)); }
public static SyntaxTriviaList CreateChildNodeIdentTrivia(this SyntaxNode node) { //calculate indent int indentLength = 4; string indent; SyntaxTriviaList leadingTrivia = node.GetLeadingTrivia(); if (leadingTrivia != null) { indent = leadingTrivia.ToString(); int newLinePos = indent.LastIndexOf("/n"); if (newLinePos >= 0) { indent = indent.Substring(newLinePos + 1); } indentLength += indent.Length; } indent = "".PadLeft(indentLength); return(SyntaxFactory.ParseLeadingTrivia(indent, 0)); }
private static bool AreTriviaEqual(SyntaxTriviaList left, SyntaxTriviaList right) { return(string.Equals(left.ToString(), right.ToString(), StringComparison.Ordinal)); }
public static TestResult HasValidCopyright(this SyntaxTriviaList leadingTrivia, int year = -1, string filePath = "") { if (leadingTrivia == null) { return(Create.TestResult(TestStatus.Pass)); } bool checkAllYears = false; if (year == -1) { checkAllYears = true; year = 2018; //Start } string documentationLink = "HasValidCopyright"; int maxYear = DateTime.Now.Year; //Max string copyrightStatement = $@"/* * This file is part of the Buildings and Habitats object Model (BHoM) * Copyright (c) 2015 - {year}, the respective contributors. All rights reserved. * * Each contributor holds copyright over their respective contributions. * The project versioning (Git) records all such contribution source information. * * * The BHoM is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3.0 of the License, or * (at your option) any later version. * * The BHoM is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. */"; string l = leadingTrivia.ToString(); l = l.Replace('\r', ' '); copyrightStatement = copyrightStatement.Replace('\r', ' '); string[] split = l.Split('\n'); string[] copyrightSplit = copyrightStatement.Split('\n'); if (split.Length < copyrightSplit.Length) { Error e = Create.Error("Copyright message is not accurate at line " + 1, Create.Location(filePath, Create.LineSpan(1, 2)), documentationLink, TestStatus.Error); return(Create.TestResult(TestStatus.Error, new List <Error> { e })); } if (!checkAllYears) { for (int x = 0; x < copyrightSplit.Length; x++) { if (split[x].TrimEnd() != copyrightSplit[x].TrimEnd()) { Error e = Create.Error("Copyright message is not accurate at line " + (x + 1), Create.Location(filePath, Create.LineSpan(x + 1, x + 2)), documentationLink, TestStatus.Error); return(Create.TestResult(TestStatus.Error, new List <Error> { e })); } } } else { List <int> availableYears = new List <int>(); for (int x = 2018; x <= maxYear; x++) { availableYears.Add(x); } for (int x = 0; x < copyrightSplit.Length; x++) { if (x == 2) { continue; //Skip the year line } if (split[x].TrimEnd() != copyrightSplit[x].TrimEnd()) { Error e = Create.Error("Copyright message is not accurate at line " + (x + 1), Create.Location(filePath, Create.LineSpan(x + 1, x + 2)), documentationLink, TestStatus.Error); return(Create.TestResult(TestStatus.Error, new List <Error> { e })); } } bool validOnOneYear = false; foreach (int a in availableYears) { copyrightSplit[2] = $" * Copyright (c) 2015 - {a}, the respective contributors. All rights reserved."; if (split[2].TrimEnd() == copyrightSplit[2].TrimEnd()) { validOnOneYear = true; } } if (!validOnOneYear) { Error e = Create.Error("Copyright message is not accurate at line 3", Create.Location(filePath, Create.LineSpan(3, 4)), documentationLink, TestStatus.Error); return(Create.TestResult(TestStatus.Error, new List <Error> { e })); } } return(Create.TestResult(TestStatus.Pass)); }
public bool SuggestReplaceWithInheritDoc() { SyntaxTriviaList trivia = GetTargetMemberTrivia(); return(HasDocComments() && !trivia.ToString().Contains("inheritdoc")); }