Skip to content

Commit

Permalink
restore performance of vscode extension (#436)
Browse files Browse the repository at this point in the history
* also fix check for syntax errors manually via context menu
  • Loading branch information
ChristopherHX authored Nov 12, 2024
1 parent 868dd31 commit 37d10e6
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 64 deletions.
3 changes: 2 additions & 1 deletion src/Runner.Language.Server/AutoCompleter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public async Task<CompletionList> Handle(CompletionParams request, CancellationT
TraceWriter = new GitHub.DistributedTask.ObjectTemplating.EmptyTraceWriter(),
Flags = GitHub.DistributedTask.Expressions2.ExpressionFlags.DTExpressionsV1 | GitHub.DistributedTask.Expressions2.ExpressionFlags.ExtendedDirectives | GitHub.DistributedTask.Expressions2.ExpressionFlags.AllowAnyForInsert,
Column = column,
Row = row
Row = row,
RawMapping = true
};
GitHub.DistributedTask.ObjectTemplating.Schema.TemplateSchema? schema = null;
try {
Expand Down
3 changes: 2 additions & 1 deletion src/Runner.Language.Server/HoverProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ private static TemplateContext CreateTemplateContext(GitHub.DistributedTask.Obje
TraceWriter = new GitHub.DistributedTask.ObjectTemplating.EmptyTraceWriter(),
Flags = GitHub.DistributedTask.Expressions2.ExpressionFlags.DTExpressionsV1 | GitHub.DistributedTask.Expressions2.ExpressionFlags.ExtendedDirectives | GitHub.DistributedTask.Expressions2.ExpressionFlags.AllowAnyForInsert,
Column = column,
Row = row
Row = row,
RawMapping = true
};
try {
var isWorkflow = request.TextDocument.Uri.Path.Contains("/.github/workflows/");
Expand Down
1 change: 1 addition & 0 deletions src/Runner.Language.Server/SemanticTokenHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private static TemplateContext CreateTemplateContext(GitHub.DistributedTask.Obj
FileProvider = new DefaultInMemoryFileProviderFileProvider(files.ToArray()),
TraceWriter = new GitHub.DistributedTask.ObjectTemplating.EmptyTraceWriter(),
Flags = GitHub.DistributedTask.Expressions2.ExpressionFlags.DTExpressionsV1 | GitHub.DistributedTask.Expressions2.ExpressionFlags.ExtendedDirectives | GitHub.DistributedTask.Expressions2.ExpressionFlags.AllowAnyForInsert,
RawMapping = true
};
TemplateToken? token = null;
List<int>? semTokens = null;
Expand Down
6 changes: 3 additions & 3 deletions src/Sdk/AzurePipelines/AzureDevops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ private static ((int, int)[], Dictionary<(int, int), int>) CreateIdxMapping(Temp

var scanner = new YamlDotNet.Core.Scanner(new StringReader(xraw), true);
try {
while(scanner.MoveNext()) {
while(scanner.MoveNext() && !(scanner.Current is YamlDotNet.Core.Tokens.Error)) {
if(scanner.Current is YamlDotNet.Core.Tokens.Scalar s) {
var x = s.Value;
var m = x.IndexOf(C);
Expand Down Expand Up @@ -765,7 +765,7 @@ private static int GetIdxOfExpression(LiteralToken lit, int row, int column)

var scanner = new YamlDotNet.Core.Scanner(new StringReader(xraw), true);
try {
while(scanner.MoveNext()) {
while(scanner.MoveNext() && !(scanner.Current is YamlDotNet.Core.Tokens.Error)) {
if(scanner.Current is YamlDotNet.Core.Tokens.Scalar s) {
var x = s.Value;
return x.IndexOf(C);
Expand Down Expand Up @@ -810,7 +810,7 @@ private static int GetIdxOfExpression(LiteralToken lit, int row, int column)
var fileId = templateContext.GetFileId(errorTemplateFileName);

// preserveString is needed for azure pipelines compatibility of the templateContext property all boolean and number token are casted to string without loosing it's exact string value
var yamlObjectReader = new YamlObjectReader(fileId, fileContent, preserveString: true, forceAzurePipelines: true);
var yamlObjectReader = new YamlObjectReader(fileId, fileContent, preserveString: true, forceAzurePipelines: true, rawMapping: context.RawMapping);
TemplateToken token = TemplateReader.Read(templateContext, schemaName ?? "pipeline-root", yamlObjectReader, fileId, out _);

context.SemTokens = templateContext.SemTokens;
Expand Down
2 changes: 2 additions & 0 deletions src/Sdk/AzurePipelines/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class Context {
internal List<AutoCompleteEntry> AutoCompleteMatches { get; set; }
public List<int> SemTokens { get; internal set; }

public bool RawMapping { get; set; }

public Context Clone() {
return MemberwiseClone() as Context;
}
Expand Down
68 changes: 15 additions & 53 deletions src/Sdk/DTObjectTemplating/ObjectTemplating/TemplateReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -594,20 +594,20 @@ private ScalarToken ParseScalar(
LiteralToken token,
DefinitionInfo definitionInfo)
{
AutoCompleteEntry completion = null;//m_context.Row >= token.Line
completion = new AutoCompleteEntry {
Depth = m_memory.Depth,
Token = token,
AllowedContext = definitionInfo.AllowedContext,
Definitions = new [] { definitionInfo.Definition }
};
if(m_context.AutoCompleteMatches != null && Match(token) && (token.PostWhiteSpace == null || MatchPost(token) /*(m_context.Row < token.PostWhiteSpace.Line && !(token.PostWhiteSpace.Line == m_context.Row && token.PostWhiteSpace.Character > m_context.Column))*/)) {
m_context.AutoCompleteMatches.RemoveAll(m => m.Depth >= m_memory.Depth);
m_context.AutoCompleteMatches.Add(completion);
} else {
completion.SemTokensOnly = true;
}
AutoCompleteEntry completion = null;
if(token is LiteralToken lit && lit.RawData != null) {
completion = new AutoCompleteEntry {
Depth = m_memory.Depth,
Token = token,
AllowedContext = definitionInfo.AllowedContext,
Definitions = new [] { definitionInfo.Definition }
};
if(m_context.AutoCompleteMatches != null && Match(token) && (token.PostWhiteSpace == null || MatchPost(token) /*(m_context.Row < token.PostWhiteSpace.Line && !(token.PostWhiteSpace.Line == m_context.Row && token.PostWhiteSpace.Character > m_context.Column))*/)) {
m_context.AutoCompleteMatches.RemoveAll(m => m.Depth >= m_memory.Depth);
m_context.AutoCompleteMatches.Add(completion);
} else {
completion.SemTokensOnly = true;
}
var rand = new Random();
string C = "CX";
while(lit.RawData.Contains(C)) {
Expand Down Expand Up @@ -692,7 +692,7 @@ private ScalarToken ParseScalar(
// An expression starts here:
if (i == startExpression)
{
if(completion.Mapping?.Length > startExpression && token.Line != null && token.Column != null) {
if(completion?.Mapping?.Length > startExpression && token.Line != null && token.Column != null) {
var (r, c) = completion.Mapping[startExpression];
m_context.AddSemToken(r, c, 3, 5, 0);
}
Expand Down Expand Up @@ -739,7 +739,7 @@ private ScalarToken ParseScalar(
endExpression - startExpression + 1 - TemplateConstants.OpenExpression.Length - TemplateConstants.CloseExpression.Length);
var expression = ParseExpression(completion, token.Line, token.Column, rawExpression, allowedContext, out Exception ex);

if(completion.Mapping?.Length >= endExpression - 1 && hasEnd && token.Line != null && token.Column != null) {
if(completion?.Mapping?.Length >= endExpression - 1 && hasEnd && token.Line != null && token.Column != null) {
var (r, c) = completion.Mapping[endExpression - 1];
m_context.AddSemToken(r, c, 2, 5, 0);
}
Expand Down Expand Up @@ -901,44 +901,6 @@ private bool AutoCompleteExpression(AutoCompleteEntry completion, int offset, st
return true;
}

private static int GetIdxOfExpression(LiteralToken lit, int row, int column)
{
var lc = column - lit.Column;
var lr = row - lit.Line;
var rand = new Random();
string C = "CX";
while(lit.RawData.Contains(C)) {
C = rand.Next(255).ToString("X2");
}
var xraw = lit.RawData;
var idx = 0;
for(int i = 0; i < lr; i++) {
var n = xraw.IndexOf('\n', idx);
if(n == -1) {
return -1;
}
idx = n + 1;
}
idx += idx == 0 ? lc ?? 0 : column - 1;
if(idx > xraw.Length) {
return -1;
}
xraw = xraw.Insert(idx, C);

var scanner = new YamlDotNet.Core.Scanner(new StringReader(xraw), true);
try {
while(scanner.MoveNext()) {
if(scanner.Current is YamlDotNet.Core.Tokens.Scalar s) {
var x = s.Value;
return x.IndexOf(C);
}
}
} catch {

}
return -1;
}

private ExpressionToken ParseExpression(
AutoCompleteEntry completion,
Int32? line,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ internal YamlObjectReader(
bool yamlFold = false,
bool yamlMerge = false,
bool preserveString = false,
bool forceAzurePipelines = false) : this(fileId, new StringReader(input), yamlAnchors, yamlFold, yamlMerge, preserveString, forceAzurePipelines)
bool forceAzurePipelines = false,
bool rawMapping = false) : this(fileId, new StringReader(input), yamlAnchors, yamlFold, yamlMerge, preserveString, forceAzurePipelines)
{
m_rawInput = input;
m_rawInput = rawMapping ? input : null;
}

private string GetScalarStringValue(Scalar scalar) {
Expand Down
4 changes: 4 additions & 0 deletions src/azure-pipelines-vscode-ext/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### v0.2.3
- fix degraded performance due to disabled new features added in the v0.2.x extension
- fix Check Manually for Syntax Errors

### v0.2.2
- fix an issue that could cause the v0.2.x extension to hang forever

Expand Down
5 changes: 3 additions & 2 deletions src/azure-pipelines-vscode-ext/ext-core/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,16 @@ public static async Task<string> ExpandCurrentPipeline(JSObject handle, string c

[MethodImpl(MethodImplOptions.NoInlining)]
[JSExport]

Check warning on line 133 in src/azure-pipelines-vscode-ext/ext-core/Program.cs

View workflow job for this annotation

GitHub Actions / deploy

This call site is reachable on all platforms. 'JSExportAttribute' is only supported on: 'browser'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 133 in src/azure-pipelines-vscode-ext/ext-core/Program.cs

View workflow job for this annotation

GitHub Actions / deploy

This call site is reachable on all platforms. 'JSExportAttribute' is only supported on: 'browser'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
public static async Task ParseCurrentPipeline(JSObject handle, string currentFileName, string schemaName, int column, int row) {
public static async Task ParseCurrentPipeline(JSObject handle, string currentFileName, string schemaName, int column, int row, bool rawMapping) {
var context = new Context {
FileProvider = new MyFileProvider(handle),
TraceWriter = new TraceWriter(handle),
Flags = GitHub.DistributedTask.Expressions2.ExpressionFlags.DTExpressionsV1 | GitHub.DistributedTask.Expressions2.ExpressionFlags.ExtendedDirectives,
RequiredParametersProvider = new RequiredParametersProvider(handle),
VariablesProvider = new VariablesProvider { Variables = new Dictionary<string, string>() },
Column = column,
Row = row
Row = row,
RawMapping = rawMapping
};
var check = column == 0 && row == 0;
try {
Expand Down
2 changes: 1 addition & 1 deletion src/azure-pipelines-vscode-ext/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ function activate(context) {
handle.enableSemTokens = autocompletelist && ('enableSemTokens' in autocompletelist);

var result = syntaxOnly
? await runtime.BINDING.bind_static_method("[ext-core] MyClass:ParseCurrentPipeline")(handle, filename, schema, pos ? pos.character + 1 : 0, pos ? pos.line + 1 : 0)
? await runtime.BINDING.bind_static_method("[ext-core] MyClass:ParseCurrentPipeline")(handle, filename, schema ?? null, pos ? pos.character + 1 : 0, pos ? pos.line + 1 : 0, (handle.enableSemTokens || pos) ? true : false)
: await runtime.BINDING.bind_static_method("[ext-core] MyClass:ExpandCurrentPipeline")(handle, filename, JSON.stringify(variables), JSON.stringify(parameters), (error && true) == true, schema)

if(pos) {
Expand Down
2 changes: 1 addition & 1 deletion src/azure-pipelines-vscode-ext/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"Programming Languages"
],
"icon": "pipeline-rocket.png",
"version": "0.2.2",
"version": "0.2.3",
"publisher": "christopherhx",
"repository": "https://github.com/ChristopherHX/runner.server",
"engines": {
Expand Down

0 comments on commit 37d10e6

Please sign in to comment.