From 0cd8cb03fae070d73f1939bd137c7e8b5ad49b99 Mon Sep 17 00:00:00 2001 From: Peter Kurhajec <61538034+PTKu@users.noreply.github.com> Date: Wed, 1 May 2024 09:53:51 +0200 Subject: [PATCH] Additional filtering of namespaces (#305) * The most significant changes involve the addition of a foreach loop in the `CsOnlinerSourceBuilder` and `CsPlainSourceBuilder` classes, which iterates over the `UsingDirectives` of the `fileSyntax` object and adds each `UsingDirective` to the source code with the appropriate namespace. Additionally, the `class_extended_by_known_type.g.cs` and `file_with_usings.g.cs` files have been updated to include more `using` directives, importing namespaces related to `Simatic.Ax.Stateframework`, `Simatic.Ax.StatePattern`, and various `FileWithUsings` namespaces. 1. The `CsOnlinerSourceBuilder` and `CsPlainSourceBuilder` classes have been updated to include a foreach loop that iterates over the `UsingDirectives` of the `fileSyntax` object. This loop adds each `UsingDirective` to the source code with the appropriate namespace. The `CsOnlinerSourceBuilder` adds the namespace as is, while the `CsPlainSourceBuilder` prepends "Pocos." to the namespace. 2. The `class_extended_by_known_type.g.cs` and `file_with_usings.g.cs` files have been updated to include additional `using` directives. These directives import namespaces related to the `Simatic.Ax.Stateframework`, `Simatic.Ax.StatePattern`, and various `FileWithUsings` namespaces. 3. The `class_extended_by_known_type.g.cs` and `file_with_usings.g.cs` files have also been updated to include additional `using` directives under the `Pocos` namespace. These directives import the same namespaces as above, but under the `Pocos` namespace. * The most significant changes involve the modification of several methods to change the parameter type from `ISyntaxToken` to `PragmaSyntax`. This includes the `AddToDictionaryIfLocalizedString`, `IsPragmaToken`, and `IsStringToken` methods. Additionally, the `IterateSyntaxTreeForPragmas` method has been uncommented and modified, and the `IterateSyntaxTreeForStringLiterals` method has been commented out. The `IsStringToken` method now checks for different `SyntaxKind` types. 1. The `IterateSyntaxTreeForStringLiterals` method has been commented out, disabling its execution. 2. The `IterateSyntaxTreeForPragmas` method has been uncommented and modified. The `token` variable now refers to `pragmaSyntax` and the `IsAttributeNamePragmaToken` method checks the `PragmaContent` of the `token`. 3. The `AddToDictionaryIfLocalizedString` method's parameter type has been changed from `ISyntaxToken` to `PragmaSyntax`. The `TryToGetLocalizedStrings` method now checks the `PragmaContent` of the `token`. 4. The `pos` variable in the `AddToDictionaryIfLocalizedString` method now gets its value from `token.SourceText.GetLineSpan(token.Span).StartLinePosition`. 5. The `IsPragmaToken` and `IsStringToken` methods' parameter type has been changed from `ISyntaxToken` to `PragmaSyntax`. The check for `SyntaxKind.PragmaToken` in `IsPragmaToken` has been commented out. 6. The `IsStringToken` method now checks if the `SyntaxKind` of the `token` is `TypedStringDToken`, `TypedStringSToken`, `UntypedStringDToken`, or `UntypedStringSToken`. * The most significant changes involve updates to the `CsOnlinerSourceBuilder` and `CsPlainSourceBuilder` classes, which now filter `UsingDirectives` based on the presence of the namespace's fully qualified name in the semantic tree of the compilation. This is achieved through a LINQ query. Changes: 1. The `CsOnlinerSourceBuilder` and `CsPlainSourceBuilder` classes have been updated to filter `UsingDirectives` based on whether the fully qualified name of the namespace is present in the semantic tree of the compilation. This is done using a LINQ query that checks if the `FullyQualifiedName` of each namespace in the semantic tree is contained in the `QualifiedIdentifierList` of the `UsingDirectives`. * asp --- .../src/AXSharp.Cs.Compiler/Onliner/CsOnlinerSourceBuilder.cs | 4 +++- .../src/AXSharp.Cs.Compiler/Plain/CsPlainSourceBuilder.cs | 4 +++- .../expected/.g/Onliners/class_extended_by_known_type.g.cs | 2 -- .../units/expected/.g/POCO/class_extended_by_known_type.g.cs | 2 -- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Onliner/CsOnlinerSourceBuilder.cs b/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Onliner/CsOnlinerSourceBuilder.cs index 964e6ce3..1716d4a6 100644 --- a/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Onliner/CsOnlinerSourceBuilder.cs +++ b/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Onliner/CsOnlinerSourceBuilder.cs @@ -55,7 +55,9 @@ public void CreateFile(IFileSyntax fileSyntax, IxNodeVisitor visitor) AddToSource("using System.Collections.Generic;"); AddToSource("using AXSharp.Connector.Localizations;"); - foreach (var fileSyntaxUsingDirective in fileSyntax.UsingDirectives) + + foreach (var fileSyntaxUsingDirective in fileSyntax.UsingDirectives + .Where(p => this.Compilation.GetSemanticTree().Namespaces.Select(p => p.FullyQualifiedName).Contains(p.QualifiedIdentifierList.GetText()))) { AddToSource($"using {fileSyntaxUsingDirective.QualifiedIdentifierList.GetText()};"); } diff --git a/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Plain/CsPlainSourceBuilder.cs b/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Plain/CsPlainSourceBuilder.cs index 0ed3172a..e38cdac0 100644 --- a/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Plain/CsPlainSourceBuilder.cs +++ b/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Plain/CsPlainSourceBuilder.cs @@ -180,7 +180,9 @@ public void CreateFile(IFileSyntax fileSyntax, IxNodeVisitor visitor) { AddToSource("using System;"); - foreach (var fileSyntaxUsingDirective in fileSyntax.UsingDirectives) + foreach (var fileSyntaxUsingDirective in + fileSyntax.UsingDirectives + .Where(p => this.Compilation.GetSemanticTree().Namespaces.Select(p => p.FullyQualifiedName).Contains(p.QualifiedIdentifierList.GetText()))) { AddToSource($"using Pocos.{fileSyntaxUsingDirective.QualifiedIdentifierList.GetText()};"); } diff --git a/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/Onliners/class_extended_by_known_type.g.cs b/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/Onliners/class_extended_by_known_type.g.cs index da3314b6..e296c211 100644 --- a/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/Onliners/class_extended_by_known_type.g.cs +++ b/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/Onliners/class_extended_by_known_type.g.cs @@ -3,8 +3,6 @@ using AXSharp.Connector.ValueTypes; using System.Collections.Generic; using AXSharp.Connector.Localizations; -using Simatic.Ax.Stateframework; -using Simatic.Ax.StatePattern; namespace Simatic.Ax.StateFramework { diff --git a/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/POCO/class_extended_by_known_type.g.cs b/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/POCO/class_extended_by_known_type.g.cs index c2efaa77..d0264b84 100644 --- a/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/POCO/class_extended_by_known_type.g.cs +++ b/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/POCO/class_extended_by_known_type.g.cs @@ -1,6 +1,4 @@ using System; -using Pocos.Simatic.Ax.Stateframework; -using Pocos.Simatic.Ax.StatePattern; namespace Pocos {