From: Jean-Philippe Bruyère Date: Fri, 28 Feb 2025 19:12:42 +0000 (+0100) Subject: save commit X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=9d201b8b2440d25c72c1b22dc62a6cce21218327;p=jp%2Fcrowedit.git save commit --- diff --git a/CrowEditBase/src/SourceEditor.cs b/CrowEditBase/src/SourceEditor.cs index 410a9d9..aba2f95 100644 --- a/CrowEditBase/src/SourceEditor.cs +++ b/CrowEditBase/src/SourceEditor.cs @@ -74,7 +74,11 @@ namespace Crow protected void tryGetSuggestions () { if (currentLoc.HasValue && Document is SourceDocument srcDoc && srcDoc.IsParsed) { int pos = srcDoc.GetAbsolutePosition(CurrentLoc.Value); - Suggestions = srcDoc.GetSuggestions (pos, currentTokenIndex, currentNode, CurrentLoc.Value); + var tmp = srcDoc.GetSuggestions (pos, currentTokenIndex, currentNode, CurrentLoc.Value); + if (tmp?.Count == 1 && tmp[0].TryCast(out Suggestion sug) && sug.Change.HasNoEffect(srcDoc.source)) + Suggestions = null; + else + Suggestions = tmp; } else Suggestions = null; } diff --git a/plugins/CECrowPlugin/src/Parsing/IML/ImlDocument.cs b/plugins/CECrowPlugin/src/Parsing/IML/ImlDocument.cs index 3a11c80..37f4c20 100644 --- a/plugins/CECrowPlugin/src/Parsing/IML/ImlDocument.cs +++ b/plugins/CECrowPlugin/src/Parsing/IML/ImlDocument.cs @@ -62,6 +62,12 @@ namespace CECrowPlugin => new Suggestion(t.Name, new TextChange(change.Start, change.Length, t.Name + change.ChangedText), endPosOffset)); } + protected override IEnumerable getAttributeNameSuggestions(string eltName, string curName, TextChange change) { + int endPosOffset = change.HasNewText ? -1 : 0; + return getAllCrowTypeMembers(eltName).Select(m + => new Suggestion(m.Name, + new TextChange(change.Start, change.Length, m.Name + change.ChangedText), endPosOffset)); + } /*public override IList GetSuggestions (int absoluteTextPos, int currentTokenIndex, SyntaxNode CurrentNode, CharLocation loc) { IList sugs = base.GetSuggestions (absoluteTextPos, currentTokenIndex, CurrentNode, loc); if (sugs != null) diff --git a/plugins/CEXmlPlugin/src/Parsing/XmlDocument.cs b/plugins/CEXmlPlugin/src/Parsing/XmlDocument.cs index c45b753..61bdbc3 100644 --- a/plugins/CEXmlPlugin/src/Parsing/XmlDocument.cs +++ b/plugins/CEXmlPlugin/src/Parsing/XmlDocument.cs @@ -28,61 +28,94 @@ namespace CrowEdit.Xml protected override SyntaxAnalyser CreateSyntaxAnalyser() => new XmlSyntaxAnalyser (this); public override string GetTokenTypeString (TokenType tokenType) => ((XmlTokenType)tokenType).ToString(); - protected virtual IEnumerable getElementNameSuggestions(string curName, TextChange change) { - - return null; - } + protected virtual IEnumerable getElementNameSuggestions(string curName, TextChange change) => null; + protected virtual IEnumerable getAttributeNameSuggestions(string eltName, string curName, TextChange change) => null; public override IList GetSuggestions (int absoluteTextPos, int currentTokenIndex, SyntaxNode CurrentNode, CharLocation loc) { - Token tok = GetTokenByIndex(currentTokenIndex); + Token tok = GetTokenByIndex(currentTokenIndex); + if (tok.Start != absoluteTextPos //middle of edited tok + && tok.End != absoluteTextPos) //occurs when curTok is last tok of text + { + return null; + } Token prevTok = GetTokenByIndex(currentTokenIndex-1); if (CurrentNode is ElementEndTagSyntax eltEndTag) { - if (!prevTok.Is(XmlTokenType.ClosingSign) && + //sug = corresponding eltName + if (!tok.Is(XmlTokenType.ElementName) && eltEndTag.Parent is ElementSyntax elt && elt.StartTag?.Name != null) { Suggestion sug = new Suggestion(elt.StartTag.Name); string curEndName = null; - - - if (tok.Is(XmlTokenType.ElementName)) { - curEndName = root.GetTokenString(tok); - sug.Change = new TextChange (tok.Start, tok.Length, sug.Caption); - } else if (prevTok.Is(XmlTokenType.ElementName)) { + if (prevTok.Is(XmlTokenType.ElementName) && !elt.StartTag.Name.Equals(curEndName, StringComparison.Ordinal)) { curEndName = root.GetTokenString(prevTok); sug.Change = new TextChange (prevTok.Start, prevTok.Length, sug.Caption); } else if (prevTok.Is(XmlTokenType.EndElementOpen)) { curEndName = ""; sug.Change = new TextChange (prevTok.End, 0, sug.Caption); - } + } else + return null; - if (!(tok.Is(XmlTokenType.ClosingSign) || sug.Change.IsEmpty || - GetTokenByIndex(currentTokenIndex+1).Is(XmlTokenType.ClosingSign))) + //if (!tok.Is(XmlTokenType.ClosingSign)) + if (!eltEndTag.close.HasValue) sug.Change.ChangedText += ">"; - if (curEndName != null && elt.StartTag.Name.StartsWith ( - curEndName, StringComparison.OrdinalIgnoreCase) - && !elt.StartTag.Name.Equals(curEndName, StringComparison.Ordinal)) + if (elt.StartTag.Name.StartsWith (curEndName, StringComparison.OrdinalIgnoreCase)) return new List ([sug]); } } else if (CurrentNode is ElementStartTagSyntax eltStartTag) { - TextChange change = default; - if (tok.Is(XmlTokenType.ElementName)) - change = new TextChange (tok.Start, tok.Length); - else if (prevTok.Is(XmlTokenType.ElementName)) - change = new TextChange (prevTok.Start, prevTok.Length); - else if (tok.Is(XmlTokenType.ElementOpen)) - change = new TextChange (tok.End, 0); - else if (prevTok.Is(XmlTokenType.ElementOpen)) - change = new TextChange (prevTok.End, 0); - else - return null; - - if (!(tok.Is(XmlTokenType.ClosingSign) || - GetTokenByIndex(currentTokenIndex+1).Is(XmlTokenType.ClosingSign))) + if (!tok.Is(XmlTokenType.ElementName)) { + TextChange change = default; + + if (prevTok.Is(XmlTokenType.ElementName)) + change = new TextChange (prevTok.Start, prevTok.Length); + else if (prevTok.Is(XmlTokenType.ElementOpen)) + change = new TextChange (prevTok.End, 0); + else if (prevTok.Type.HasFlag(TokenType.Trivia) && eltStartTag.name.HasValue) { + //attribute + change = new TextChange(tok.Start, 0); + if (!tok.Is(XmlTokenType.EqualSign)) + change.ChangedText += "=\"\""; + return getAttributeNameSuggestions(eltStartTag.Name, null, change).ToList(); + + } else + return null; + + //if (!tok.Is(XmlTokenType.ClosingSign)) + if (!eltStartTag.close.HasValue) change.ChangedText = ">"; - - return getElementNameSuggestions(eltStartTag.Name, change).ToList(); + + return getElementNameSuggestions(eltStartTag.Name, change).ToList(); + + } + } else if (CurrentNode is AttributeSyntax attrib) { + if (prevTok.Is(XmlTokenType.AttributeName)) { + + } + + /*if (tokType == XmlTokenType.AttributeName) { + if (attrib.ValueToken.HasValue) { + change = new TextChange (tok.Start, tok.Length, selectedSugg); + newSelection = new TextSpan( + attrib.ValueToken.Value.Start + change.CharDiff + 1, + attrib.ValueToken.Value.End + change.CharDiff - 1 + ); + } else { + change = new TextChange (tok.Start, tok.Length, selectedSugg + "=\"\""); + newSelection = TextSpan.FromStartAndLength (tok.Start + selectedSugg.Length + 2); + } + } else { + int offset = 1; + if (!attrib.valueClose.HasValue) { + selectedSugg += root.GetTokenStringByIndex(attrib.valueClose.Value); + offset = 0; + } + if (tokType == XmlTokenType.AttributeValueOpen) + change = new TextChange (tok.End, 0, selectedSugg); + else if (tokType == XmlTokenType.AttributeValue) + change = new TextChange (tok.Start, tok.Length, selectedSugg); + newSelection = TextSpan.FromStartAndLength (change.End2 + offset); + }*/ } return null; @@ -121,29 +154,7 @@ namespace CrowEdit.Xml newSelection = TextSpan.FromStartAndLength (change.End2 - 1); } } else if (node is AttributeSyntax attrib) { - if (tokType == XmlTokenType.AttributeName) { - if (attrib.ValueToken.HasValue) { - change = new TextChange (tok.Start, tok.Length, selectedSugg); - newSelection = new TextSpan( - attrib.ValueToken.Value.Start + change.CharDiff + 1, - attrib.ValueToken.Value.End + change.CharDiff - 1 - ); - } else { - change = new TextChange (tok.Start, tok.Length, selectedSugg + "=\"\""); - newSelection = TextSpan.FromStartAndLength (tok.Start + selectedSugg.Length + 2); - } - } else { - int offset = 1; - if (!attrib.valueClose.HasValue) { - selectedSugg += root.GetTokenStringByIndex(attrib.valueClose.Value); - offset = 0; - } - if (tokType == XmlTokenType.AttributeValueOpen) - change = new TextChange (tok.End, 0, selectedSugg); - else if (tokType == XmlTokenType.AttributeValue) - change = new TextChange (tok.Start, tok.Length, selectedSugg); - newSelection = TextSpan.FromStartAndLength (change.End2 + offset); - } + } else if (tokType == XmlTokenType.ElementOpen) { change = new TextChange (tok.End, 0, selectedSugg + " "); } else