]> O.S.I.I.S - jp/crowedit.git/commitdiff
use currentTokenIndex to call for suggestion to easily get previous token
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Thu, 27 Feb 2025 20:20:33 +0000 (21:20 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Thu, 27 Feb 2025 20:20:33 +0000 (21:20 +0100)
CrowEditBase/src/Compiler/SourceDocument.cs
CrowEditBase/src/SourceEditor.cs
plugins/CECrowPlugin/src/Parsing/IML/ImlDocument.cs
plugins/CECrowPlugin/src/Parsing/Styling/StyleDocument.cs
plugins/CEEbnfPlugin/src/Parsing/EbnfDocument.cs
plugins/CERoslynPlugin/src/CSDocument.cs
plugins/CEXmlPlugin/src/Parsing/XmlDocument.cs

index 24f9068a2036f0e9d9b4a3d99a6cf95af062cf49..5c746661dadebb90bc86d4d321a6ba7b8e21ff21 100644 (file)
@@ -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);
 
                /// <summary>
                /// complete current token with selected item from the suggestion overlay.
index 9f9bf24df6a5d4f3d10f0eacaad337a3225a86e3..b951b3fec919344277fde80ddf577bd981a59c72 100644 (file)
@@ -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)) ||
index acb0487fcd7c3c0a9a55c7612a6a83390dc91c24..4b3afcb1c587a4eb78ac48bf41d97764a63e165b 100644 (file)
@@ -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<string> (allWidgetNames);
index ac39b231102327d02fb2a88200991c5e2eee343b..0fbee53e3f4568033198df92eb9e23efd1b9255a 100644 (file)
@@ -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();*/
index e5494bf31775a44403597018bc648c1872ec5157..0ac5675000ab595c4760aeb7160bb7bf117cd194 100644 (file)
@@ -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) {
index 6a68f3913f016aeca9e223b82373160c3e2867d0..8c8bf14cd44b5e197c89f2c5ca1b6cbf5d0188c3 100644 (file)
@@ -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)
index 196825cfa2a840f01137c3743d6bb3c4e2ca6f36..ac7ec9bf243b4719fa212ecb0ce846a1b6d8c983 100644 (file)
@@ -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<string> (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<string> (new string[] {eltName});
+                       }
                        return null;
                }
                public override bool TryCompleteToken (Token tok, SyntaxNode node, object suggestion, out TextChange change, out TextSpan? newSelection) {