diff --git a/Source/MDK/Build/AnalysisExtensions.cs b/Source/MDK/Build/AnalysisExtensions.cs index 14d67d7..f049a21 100644 --- a/Source/MDK/Build/AnalysisExtensions.cs +++ b/Source/MDK/Build/AnalysisExtensions.cs @@ -44,6 +44,45 @@ 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 2cbdaef..f44af42 100644 --- a/Source/MDK/Build/DocumentAnalysis/DocumentAnalyzer.cs +++ b/Source/MDK/Build/DocumentAnalysis/DocumentAnalyzer.cs @@ -136,45 +136,46 @@ public override void VisitEnumDeclaration(EnumDeclarationSyntax node) public override void VisitClassDeclaration(ClassDeclarationSyntax node) { if (node.GetFullName(DeclarationFullNameFlags.WithoutNamespaceName) == "Program") - _parts.Add(new ProgramScriptPart(_document, MoveBraceTrivia(node), _sortWeight)); + _parts.Add(new ProgramScriptPart(_document, 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 a198426..53b741b 100644 --- a/Source/MDK/Build/DocumentAnalysis/ProgramScriptPart.cs +++ b/Source/MDK/Build/DocumentAnalysis/ProgramScriptPart.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Text; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; @@ -24,7 +25,32 @@ public ProgramScriptPart(Document document, ClassDeclarationSyntax partRoot, int /// Retrieves the leading trivia of this part. /// /// - public IEnumerable GetLeadingTrivia() => ((ClassDeclarationSyntax)PartRoot).OpenBraceToken.TrailingTrivia; + 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; + } /// /// Gets the content of this part. @@ -69,20 +95,22 @@ public override string GenerateContent() { var classDeclaration = (ClassDeclarationSyntax)PartRoot; var buffer = new StringBuilder(); - // Write opening brace trailing trivia - if (classDeclaration.OpenBraceToken.HasTrailingTrivia) - { - var trailingTrivia = classDeclaration.OpenBraceToken.TrailingTrivia; + foreach (var trivia in GetLeadingTrivia()) + buffer.Append(trivia.ToFullString()); + //// 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()) @@ -109,13 +137,16 @@ public override string GenerateContent() } } - // 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()); - } + 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()); + //} return buffer.ToString(); } diff --git a/Source/MDK/Build/Solution/ProgramDocumentComposer.cs b/Source/MDK/Build/Solution/ProgramDocumentComposer.cs index 888b93e..7239592 100644 --- a/Source/MDK/Build/Solution/ProgramDocumentComposer.cs +++ b/Source/MDK/Build/Solution/ProgramDocumentComposer.cs @@ -153,10 +153,15 @@ 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 301886c..4f21fc9 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.16"); + public static readonly Version Version = new Version("1.1.17"); /// /// The required IDE version diff --git a/Source/MDK/source.extension.vsixmanifest b/Source/MDK/source.extension.vsixmanifest index 7bfc972..ea4e1d5 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 e20aebe..40bf002 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.16"); + public static readonly Version TargetPackageVersion = new Version("1.1.17"); } } diff --git a/Source/MDKUtilities/MDKFactory.cs b/Source/MDKUtilities/MDKFactory.cs index 8a2db10..cae1994 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)