From 460d7927285e6b090806c3091150a77ff7b28ef2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Thu, 27 Feb 2025 21:20:33 +0100 Subject: [PATCH] use currentTokenIndex to call for suggestion to easily get previous token --- CrowEditBase/src/Compiler/SourceDocument.cs | 2 +- CrowEditBase/src/SourceEditor.cs | 2 +- .../src/Parsing/IML/ImlDocument.cs | 6 ++-- .../src/Parsing/Styling/StyleDocument.cs | 3 +- .../CEEbnfPlugin/src/Parsing/EbnfDocument.cs | 3 +- plugins/CERoslynPlugin/src/CSDocument.cs | 3 +- .../CEXmlPlugin/src/Parsing/XmlDocument.cs | 33 +++++++++++++------ 7 files changed, 34 insertions(+), 18 deletions(-) diff --git a/CrowEditBase/src/Compiler/SourceDocument.cs b/CrowEditBase/src/Compiler/SourceDocument.cs index 24f9068..5c74666 100644 --- a/CrowEditBase/src/Compiler/SourceDocument.cs +++ b/CrowEditBase/src/Compiler/SourceDocument.cs @@ -138,7 +138,7 @@ namespace CrowEditBase public virtual string GetTokenTypeString (TokenType tokenType) => tokenType.ToString(); //protected abstract Tokenizer CreateTokenizer (); protected abstract SyntaxAnalyser CreateSyntaxAnalyser (); - public abstract IList GetSuggestions (Token currentToken, SyntaxNode currentNode, CharLocation loc); + public abstract IList GetSuggestions (int currentTokenIndex, SyntaxNode currentNode, CharLocation loc); /// /// complete current token with selected item from the suggestion overlay. diff --git a/CrowEditBase/src/SourceEditor.cs b/CrowEditBase/src/SourceEditor.cs index 9f9bf24..b951b3f 100644 --- a/CrowEditBase/src/SourceEditor.cs +++ b/CrowEditBase/src/SourceEditor.cs @@ -62,7 +62,7 @@ namespace Crow protected void tryGetSuggestions () { if (currentLoc.HasValue && Document is SourceDocument srcDoc && srcDoc.IsParsed) { - IList suggs = srcDoc.GetSuggestions (CurrentToken, currentNode, CurrentLoc.Value); + IList suggs = srcDoc.GetSuggestions (currentTokenIndex, currentNode, CurrentLoc.Value); Token tok = CurrentToken; if (suggs != null && suggs.Count == 1 && ( (suggs[0] is System.Reflection.MemberInfo mi && mi.Name == srcDoc.GetText(tok.Span)) || diff --git a/plugins/CECrowPlugin/src/Parsing/IML/ImlDocument.cs b/plugins/CECrowPlugin/src/Parsing/IML/ImlDocument.cs index acb0487..4b3afcb 100644 --- a/plugins/CECrowPlugin/src/Parsing/IML/ImlDocument.cs +++ b/plugins/CECrowPlugin/src/Parsing/IML/ImlDocument.cs @@ -49,13 +49,13 @@ namespace CECrowPlugin return crowType.GetMember (memberName, BindingFlags.Public | BindingFlags.Instance).FirstOrDefault (); } - public override IList GetSuggestions (Token currentToken, SyntaxNode CurrentNode, CharLocation loc) { - IList sugs = base.GetSuggestions (currentToken, CurrentNode, loc); + public override IList GetSuggestions (int currentTokenIndex, SyntaxNode CurrentNode, CharLocation loc) { + IList sugs = base.GetSuggestions (currentTokenIndex, CurrentNode, loc); if (sugs != null) return sugs; //Token tok = currentToken.Length == 0 || currentToken.Type.HasFlag(TokenType.Trivia) ? previousToken : currentToken; - Token tok = currentToken; + Token tok = GetTokenByIndex(currentTokenIndex); if (tok.GetTokenType() == XmlTokenType.ElementOpen) return new List (allWidgetNames); diff --git a/plugins/CECrowPlugin/src/Parsing/Styling/StyleDocument.cs b/plugins/CECrowPlugin/src/Parsing/Styling/StyleDocument.cs index ac39b23..0fbee53 100644 --- a/plugins/CECrowPlugin/src/Parsing/Styling/StyleDocument.cs +++ b/plugins/CECrowPlugin/src/Parsing/Styling/StyleDocument.cs @@ -24,7 +24,8 @@ namespace CECrowPlugin.Style protected override SyntaxAnalyser CreateSyntaxAnalyser() => new StyleSyntaxAnalyser (this); - public override IList GetSuggestions (Token currentToken, SyntaxNode CurrentNode, CharLocation loc) { + public override IList GetSuggestions (int currentTokenIndex, SyntaxNode CurrentNode, CharLocation loc) { + Token currentToken = GetTokenByIndex(currentTokenIndex); /*Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine ($"Tok: {this.CurrentTokenString} {((StyleTokenType)CurrentToken.Type).ToString()}"); Console.ResetColor();*/ diff --git a/plugins/CEEbnfPlugin/src/Parsing/EbnfDocument.cs b/plugins/CEEbnfPlugin/src/Parsing/EbnfDocument.cs index e5494bf..0ac5675 100644 --- a/plugins/CEEbnfPlugin/src/Parsing/EbnfDocument.cs +++ b/plugins/CEEbnfPlugin/src/Parsing/EbnfDocument.cs @@ -25,7 +25,8 @@ namespace CrowEdit.Ebnf } protected override SyntaxAnalyser CreateSyntaxAnalyser() => new EbnfSyntaxAnalyser (this); - public override IList GetSuggestions (Token currentToken, SyntaxNode CurrentNode, CharLocation loc) { + public override IList GetSuggestions (int currentTokenIndex, SyntaxNode CurrentNode, CharLocation loc) { + Token currentToken = GetTokenByIndex(currentTokenIndex); return null; } public override bool TryCompleteToken (Token tok, SyntaxNode node, object suggestion, out TextChange change, out TextSpan? newSelection) { diff --git a/plugins/CERoslynPlugin/src/CSDocument.cs b/plugins/CERoslynPlugin/src/CSDocument.cs index 6a68f39..8c8bf14 100644 --- a/plugins/CERoslynPlugin/src/CSDocument.cs +++ b/plugins/CERoslynPlugin/src/CSDocument.cs @@ -46,8 +46,9 @@ namespace CERoslynPlugin #region SourceDocument abstract class implementation protected override SyntaxAnalyser CreateSyntaxAnalyser() => new CSSyntaxAnalyser (this); - public override IList GetSuggestions (Token currentToken, SyntaxNode CurrentNode, CharLocation loc) + public override IList GetSuggestions (int currentTokenIndex, SyntaxNode CurrentNode, CharLocation loc) { + Token currentToken = GetTokenByIndex(currentTokenIndex); throw new NotImplementedException(); } public override bool TryCompleteToken (Token tok, SyntaxNode node, object suggestion, out TextChange change, out TextSpan? newSelection) diff --git a/plugins/CEXmlPlugin/src/Parsing/XmlDocument.cs b/plugins/CEXmlPlugin/src/Parsing/XmlDocument.cs index 196825c..ac7ec9b 100644 --- a/plugins/CEXmlPlugin/src/Parsing/XmlDocument.cs +++ b/plugins/CEXmlPlugin/src/Parsing/XmlDocument.cs @@ -18,22 +18,35 @@ namespace CrowEdit.Xml public static void SetTokenType (this Token tok, XmlTokenType type) { tok.Type = (TokenType)type; } - + public static bool Is(this Token tok, XmlTokenType type) => (XmlTokenType)tok.Type == type; } public class XmlDocument : SourceDocument { public XmlDocument (string fullPath, string editorPath) : base (fullPath, editorPath) { } protected override SyntaxAnalyser CreateSyntaxAnalyser() => new XmlSyntaxAnalyser (this); public override string GetTokenTypeString (TokenType tokenType) => ((XmlTokenType)tokenType).ToString(); - public override IList GetSuggestions (Token currentToken, SyntaxNode CurrentNode, CharLocation loc) { - /*currentToken = FindTokenIncludingPosition (pos); - currentNode = FindNodeIncludingPosition (pos);*/ - if (currentToken.GetTokenType() == XmlTokenType.EndElementOpen && - CurrentNode is ElementEndTagSyntax eltEndTag && !eltEndTag.IsComplete) { - ElementSyntax es = eltEndTag.Parent as ElementSyntax; - if (es?.StartTag.name != null) - return new List (new string[] {es.StartTag.Name}); - } + public override IList GetSuggestions (int currentTokenIndex, SyntaxNode CurrentNode, CharLocation loc) { + Token currentToken = GetTokenByIndex(currentTokenIndex); + Token previousToken = GetTokenByIndex(currentTokenIndex-1); + + if (CurrentNode is ElementEndTagSyntax eltEndTag && + eltEndTag.Parent is ElementSyntax elt && + elt.StartTag?.Name != null) { + + string eltName = elt.StartTag.Name; + string curEndName = null; + + if (currentToken.Is(XmlTokenType.ElementName)) + curEndName = root.GetTokenString(currentToken); + else if (previousToken.Is(XmlTokenType.ElementName)) + curEndName = root.GetTokenString(previousToken); + else if (previousToken.Is(XmlTokenType.EndElementOpen)) + curEndName = ""; + + if (curEndName != null && elt.StartTag.Name.StartsWith ( + curEndName, StringComparison.OrdinalIgnoreCase)) + return new List (new string[] {eltName}); + } return null; } public override bool TryCompleteToken (Token tok, SyntaxNode node, object suggestion, out TextChange change, out TextSpan? newSelection) { -- 2.47.3