diff --git a/Source/MDK/Build/AnalysisExtensions.cs b/Source/MDK/Build/AnalysisExtensions.cs index f049a21..14d67d7 100644 --- a/Source/MDK/Build/AnalysisExtensions.cs +++ b/Source/MDK/Build/AnalysisExtensions.cs @@ -44,45 +44,6 @@ public static T Unindented(this T node, int indentations) where T: SyntaxNode return (T)rewriter.Visit(node); } - /// - /// Removes indentations from the given node if they are equal to or larger than the indicated number of indentations. - /// - /// - /// - /// - /// - public static SyntaxToken Unindented(this SyntaxToken node, int indentations) - { - var rewriter = new UnindentRewriter(indentations); - return rewriter.VisitToken(node); - } - - /// - /// Removes indentations from the given node if they are equal to or larger than the indicated number of indentations. - /// - /// - /// - /// - /// - public static SyntaxTrivia Unindented(this SyntaxTrivia node, int indentations) - { - var rewriter = new UnindentRewriter(indentations); - return rewriter.VisitTrivia(node); - } - - /// - /// Removes indentations from the given node if they are equal to or larger than the indicated number of indentations. - /// - /// - /// - /// - /// - public static SyntaxTriviaList Unindented(this SyntaxTriviaList node, int indentations) - { - var rewriter = new UnindentRewriter(indentations); - return SyntaxFactory.TriviaList(node.Select(rewriter.VisitTrivia)); - } - /// /// Retrieves the fully qualified name of the given symbol. /// diff --git a/Source/MDK/Build/DocumentAnalysis/DocumentAnalyzer.cs b/Source/MDK/Build/DocumentAnalysis/DocumentAnalyzer.cs index f44af42..2cbdaef 100644 --- a/Source/MDK/Build/DocumentAnalysis/DocumentAnalyzer.cs +++ b/Source/MDK/Build/DocumentAnalysis/DocumentAnalyzer.cs @@ -136,46 +136,45 @@ public override void VisitEnumDeclaration(EnumDeclarationSyntax node) public override void VisitClassDeclaration(ClassDeclarationSyntax node) { if (node.GetFullName(DeclarationFullNameFlags.WithoutNamespaceName) == "Program") - _parts.Add(new ProgramScriptPart(_document, node, _sortWeight)); - //_parts.Add(new ProgramScriptPart(_document, MoveBraceTrivia(node), _sortWeight)); + _parts.Add(new ProgramScriptPart(_document, MoveBraceTrivia(node), _sortWeight)); else _parts.Add(new ExtensionScriptPart(_document, node, _sortWeight)); _sortWeight++; } - //ClassDeclarationSyntax MoveBraceTrivia(ClassDeclarationSyntax node) - //{ - // // If there is some node to move the brace trivia to, do so in order - // // to have it treated correctly by the composers. Otherwise it doesn't - // // matter. - // var firstChild = node.ChildNodes().FirstOrDefault(); - // if (firstChild != null) - // { - // var trailingTrivia = node.OpenBraceToken.TrailingTrivia; - - // // Skip the whitespace and line the brace itself is on - // var i = 0; - // while (i < trailingTrivia.Count && trailingTrivia[i].Kind() == SyntaxKind.WhitespaceTrivia) - // i++; - // if (i < trailingTrivia.Count && trailingTrivia[i].Kind() == SyntaxKind.EndOfLineTrivia) - // i++; - // node = node.WithOpenBraceToken(node.OpenBraceToken.WithTrailingTrivia(SyntaxFactory.EndOfLine("\n"))); - // if (i < trailingTrivia.Count) - // { - // node = node.ReplaceNode(firstChild, firstChild.WithLeadingTrivia(firstChild.GetLeadingTrivia().InsertRange(0, trailingTrivia.Skip(i)))); - // } - // } - - // var lastChild = node.ChildNodes().LastOrDefault(); - // if (lastChild != null) - // { - // var leadingTrivia = node.CloseBraceToken.LeadingTrivia; - // node = node.WithCloseBraceToken(node.CloseBraceToken.WithLeadingTrivia()); - // node = node.ReplaceNode(lastChild, lastChild.WithTrailingTrivia(lastChild.GetTrailingTrivia().AddRange(leadingTrivia))); - // } - - // return node; - //} + ClassDeclarationSyntax MoveBraceTrivia(ClassDeclarationSyntax node) + { + // If there is some node to move the brace trivia to, do so in order + // to have it treated correctly by the composers. Otherwise it doesn't + // matter. + var firstChild = node.ChildNodes().FirstOrDefault(); + if (firstChild != null) + { + var trailingTrivia = node.OpenBraceToken.TrailingTrivia; + + // Skip the whitespace and line the brace itself is on + var i = 0; + while (i < trailingTrivia.Count && trailingTrivia[i].Kind() == SyntaxKind.WhitespaceTrivia) + i++; + if (i < trailingTrivia.Count && trailingTrivia[i].Kind() == SyntaxKind.EndOfLineTrivia) + i++; + node = node.WithOpenBraceToken(node.OpenBraceToken.WithTrailingTrivia(SyntaxFactory.EndOfLine("\n"))); + if (i < trailingTrivia.Count) + { + node = node.ReplaceNode(firstChild, firstChild.WithLeadingTrivia(firstChild.GetLeadingTrivia().InsertRange(0, trailingTrivia.Skip(i)))); + } + } + + var lastChild = node.ChildNodes().LastOrDefault(); + if (lastChild != null) + { + var leadingTrivia = node.CloseBraceToken.LeadingTrivia; + node = node.WithCloseBraceToken(node.CloseBraceToken.WithLeadingTrivia()); + node = node.ReplaceNode(lastChild, lastChild.WithTrailingTrivia(lastChild.GetTrailingTrivia().AddRange(leadingTrivia))); + } + + return node; + } } } } diff --git a/Source/MDK/Build/DocumentAnalysis/ProgramScriptPart.cs b/Source/MDK/Build/DocumentAnalysis/ProgramScriptPart.cs index 53b741b..a198426 100644 --- a/Source/MDK/Build/DocumentAnalysis/ProgramScriptPart.cs +++ b/Source/MDK/Build/DocumentAnalysis/ProgramScriptPart.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; using System.Text; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; @@ -25,32 +24,7 @@ public ProgramScriptPart(Document document, ClassDeclarationSyntax partRoot, int /// Retrieves the leading trivia of this part. /// /// - public IEnumerable GetLeadingTrivia() => SkipFirstTriviaLine(((ClassDeclarationSyntax)PartRoot).OpenBraceToken.TrailingTrivia); - - IEnumerable SkipFirstTriviaLine(SyntaxTriviaList triviaList) - { - var skipCount = FindTriviaSkipCount(triviaList); - return triviaList.Skip(skipCount); - } - - static int FindTriviaSkipCount(SyntaxTriviaList triviaList) - { - for (var index = 0; index < triviaList.Count; index++) - { - var trivia = triviaList[index]; - switch (trivia.Kind()) - { - case SyntaxKind.WhitespaceTrivia: - continue; - case SyntaxKind.EndOfLineTrivia: - return index + 1; - default: - return 0; - } - } - - return triviaList.Count; - } + public IEnumerable GetLeadingTrivia() => ((ClassDeclarationSyntax)PartRoot).OpenBraceToken.TrailingTrivia; /// /// Gets the content of this part. @@ -95,22 +69,20 @@ public override string GenerateContent() { var classDeclaration = (ClassDeclarationSyntax)PartRoot; var buffer = new StringBuilder(); - foreach (var trivia in GetLeadingTrivia()) - buffer.Append(trivia.ToFullString()); - //// Write opening brace trailing trivia - //if (classDeclaration.OpenBraceToken.HasTrailingTrivia) - //{ - // var trailingTrivia = classDeclaration.OpenBraceToken.TrailingTrivia; + // Write opening brace trailing trivia + if (classDeclaration.OpenBraceToken.HasTrailingTrivia) + { + var trailingTrivia = classDeclaration.OpenBraceToken.TrailingTrivia; - // // Skip the whitespace and line the brace itself is on - // var i = 0; - // while (i < trailingTrivia.Count && trailingTrivia[i].Kind() == SyntaxKind.WhitespaceTrivia) - // i++; - // if (i < trailingTrivia.Count && trailingTrivia[i].Kind() == SyntaxKind.EndOfLineTrivia) - // i++; - // for (; i < trailingTrivia.Count; i++) - // buffer.Append(trailingTrivia[i].ToFullString()); - //} + // Skip the whitespace and line the brace itself is on + var i = 0; + while (i < trailingTrivia.Count && trailingTrivia[i].Kind() == SyntaxKind.WhitespaceTrivia) + i++; + if (i < trailingTrivia.Count && trailingTrivia[i].Kind() == SyntaxKind.EndOfLineTrivia) + i++; + for (; i < trailingTrivia.Count; i++) + buffer.Append(trailingTrivia[i].ToFullString()); + } // Write general content foreach (var node in PartRoot.ChildNodes()) @@ -137,16 +109,13 @@ public override string GenerateContent() } } - foreach (var trivia in GetTrailingTrivia()) - buffer.Append(trivia.ToFullString()); - - //// Write closing brace opening trivia - //// Write opening brace trailing trivia - //if (classDeclaration.CloseBraceToken.HasLeadingTrivia) - //{ - // foreach (var trivia in classDeclaration.CloseBraceToken.LeadingTrivia) - // buffer.Append(trivia.ToFullString()); - //} + // Write closing brace opening trivia + // Write opening brace trailing trivia + if (classDeclaration.CloseBraceToken.HasLeadingTrivia) + { + foreach (var trivia in classDeclaration.CloseBraceToken.LeadingTrivia) + buffer.Append(trivia.ToFullString()); + } return buffer.ToString(); } diff --git a/Source/MDK/Build/Solution/ProgramDocumentComposer.cs b/Source/MDK/Build/Solution/ProgramDocumentComposer.cs index 7239592..888b93e 100644 --- a/Source/MDK/Build/Solution/ProgramDocumentComposer.cs +++ b/Source/MDK/Build/Solution/ProgramDocumentComposer.cs @@ -153,15 +153,10 @@ Document CreateProgramDocument(Project project, ProjectContent content) } } - members.AddRange(nodes.Select(node => node/*.Unindented(2)*/)); + members.AddRange(nodes.Select(node => node.Unindented(2))); } programDeclaration = programDeclaration.WithMembers(new SyntaxList().AddRange(members)); - if (pendingTrivia.Count > 0) - { - programDeclaration = programDeclaration.WithCloseBraceToken(programDeclaration.CloseBraceToken.WithLeadingTrivia(SyntaxFactory.TriviaList(pendingTrivia))); - programDeclaration = programDeclaration.WithCloseBraceToken(programDeclaration.CloseBraceToken.Unindented(2)); - } var extensionDeclarations = content.Parts .OfType() diff --git a/Source/MDK/MDKPackage.GeneratedInfo.cs b/Source/MDK/MDKPackage.GeneratedInfo.cs index 4f21fc9..9768d0e 100644 --- a/Source/MDK/MDKPackage.GeneratedInfo.cs +++ b/Source/MDK/MDKPackage.GeneratedInfo.cs @@ -8,7 +8,7 @@ public partial class MDKPackage /// /// The current package version /// - public static readonly Version Version = new Version("1.1.17"); + public static readonly Version Version = new Version("1.1.18"); /// /// The required IDE version diff --git a/Source/MDK/source.extension.vsixmanifest b/Source/MDK/source.extension.vsixmanifest index ea4e1d5..e678221 100644 --- a/Source/MDK/source.extension.vsixmanifest +++ b/Source/MDK/source.extension.vsixmanifest @@ -1,7 +1,7 @@  - + MDK/SE A toolkit to help with ingame script (programmable block) development for Keen Software House's space sandbox Space Engineers. diff --git a/Source/MDKServices/ProjectScriptInfo.GeneratedInfo.cs b/Source/MDKServices/ProjectScriptInfo.GeneratedInfo.cs index 40bf002..fd20cc4 100644 --- a/Source/MDKServices/ProjectScriptInfo.GeneratedInfo.cs +++ b/Source/MDKServices/ProjectScriptInfo.GeneratedInfo.cs @@ -7,6 +7,6 @@ partial class ProjectScriptInfo /// /// The current package version this utility assembly targets /// - public static readonly Version TargetPackageVersion = new Version("1.1.17"); + public static readonly Version TargetPackageVersion = new Version("1.1.18"); } } diff --git a/Source/MDKUtilities/MDKFactory.cs b/Source/MDKUtilities/MDKFactory.cs index cae1994..8a2db10 100644 --- a/Source/MDKUtilities/MDKFactory.cs +++ b/Source/MDKUtilities/MDKFactory.cs @@ -100,7 +100,7 @@ public static void DefaultEcho(string text) /// /// Runs the Save method of the given program, if there is any. - /// + /// /// /// public static string Save(IMyGridProgram gridProgram)