Skip to content

Commit

Permalink
improve auto complete for null object (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherHX authored Dec 2, 2024
1 parent 122f991 commit d981cf1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
78 changes: 40 additions & 38 deletions src/Sdk/DTObjectTemplating/ObjectTemplating/TemplateReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ private ScalarToken ParseScalar(
DefinitionInfo definitionInfo)
{
AutoCompleteEntry completion = null;
if(token is LiteralToken lit && lit.RawData != null) {
if(token is LiteralToken lit && (lit.PreWhiteSpace != null || lit.PostWhiteSpace != null || lit.RawData != null)) {
completion = new AutoCompleteEntry {
Depth = m_memory.Depth,
Token = token,
Expand All @@ -608,50 +608,52 @@ private ScalarToken ParseScalar(
} else {
completion.SemTokensOnly = true;
}
var rand = new Random();
string C = "CX";
while(lit.RawData.Contains(C)) {
C = rand.Next(255).ToString("X2");
}
var praw = lit.ToString();
(int, int)[] mapping = new (int, int)[praw.Length + 1];
var rmapping = new Dictionary<(int, int), int>();
Array.Fill(mapping, (-1, -1));

int column = lit.Column.Value;
int line = lit.Line.Value;
int ridx = -1;
for(int idx = 0; idx < lit.RawData.Length; idx++) {
if(lit.RawData[idx] == '\n') {
line++;
column = 1;
continue;
if(lit.RawData != null) {
var rand = new Random();
string C = "CX";
while(lit.RawData.Contains(C)) {
C = rand.Next(255).ToString("X2");
}
var xraw = lit.RawData.Insert(idx, C);

var scanner = new YamlDotNet.Core.Scanner(new StringReader(xraw), true);
try {
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);
if(m >= 0 && m < mapping.Length && ridx <= m) {
if(mapping[m] != (-1,-1)) {
rmapping.Remove(mapping[m]);
var praw = lit.ToString();
(int, int)[] mapping = new (int, int)[praw.Length + 1];
var rmapping = new Dictionary<(int, int), int>();
Array.Fill(mapping, (-1, -1));

int column = lit.Column.Value;
int line = lit.Line.Value;
int ridx = -1;
for(int idx = 0; idx < lit.RawData.Length; idx++) {
if(lit.RawData[idx] == '\n') {
line++;
column = 1;
continue;
}
var xraw = lit.RawData.Insert(idx, C);

var scanner = new YamlDotNet.Core.Scanner(new StringReader(xraw), true);
try {
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);
if(m >= 0 && m < mapping.Length && ridx <= m) {
if(mapping[m] != (-1,-1)) {
rmapping.Remove(mapping[m]);
}
mapping[m] = (line, column);
rmapping[(line, column)] = m;
ridx = m;
}
mapping[m] = (line, column);
rmapping[(line, column)] = m;
ridx = m;
}
}
}
} catch {
} catch {

}
column++;
}
column++;
completion.Mapping = mapping;
completion.RMapping = rmapping;
}
completion.Mapping = mapping;
completion.RMapping = rmapping;
}
var allowedContext = definitionInfo.AllowedContext;
var isExpression = definitionInfo.Definition is StringDefinition sdef && sdef.IsExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public Boolean AllowLiteral(out LiteralToken value)
{
value = CreateStringToken(scalar);
}
if(!string.IsNullOrEmpty(m_rawInput) && value.Type != TokenType.String) {
if(!string.IsNullOrEmpty(m_rawInput)) {
FillPreWhitespace(scalar, value);
FillPostWhitespace(scalar, value);
}
Expand Down Expand Up @@ -164,6 +164,9 @@ private LiteralToken CreateStringToken(Scalar scalar)

private void FillPreWhitespace(NodeEvent scalar, TemplateToken tkn)
{
if(tkn.PreWhiteSpace != null) {
return;
}
var lines = 0;
var column = 0;
int i = scalar.Start.Index - 1;
Expand Down Expand Up @@ -195,6 +198,9 @@ private void FillPreWhitespace(NodeEvent scalar, TemplateToken tkn)

private void FillPostWhitespace(ParsingEvent scalar, TemplateToken tkn)
{
if(tkn.PostWhiteSpace != null) {
return;
}
var lines = 0;
var column = 0;
int i = scalar.End.Index;
Expand Down

0 comments on commit d981cf1

Please sign in to comment.