Ejemplo n.º 1
0
    public static CiWinapi AddWinapi(INamedTypeSymbol typeSym, List <CiComplItem> items, TextSpan span, int typenameStart, bool onlyTypes)
    {
        Debug.Assert(IsWinapiClassSymbol(typeSym));

        //In 'new' context show only types, except interfaces.
        //Also should do the same in 'as' context, and partially in 'is' context. But Roslyn doesn't, so we too.
        var sql = "SELECT name, kind FROM api";

        if (onlyTypes)
        {
            sql += " WHERE kind<=4";                    //see CiItemKind
        }
        items.Capacity = items.Count + 60000;
        using var db   = EdDatabases.OpenWinapi();
        using var stat = db.Statement(sql);
        while (stat.Step())
        {
            string name = stat.GetText(0);
            var    kind = (CiItemKind)stat.GetInt(1);
            var    ci   = new CiComplItem(CiComplProvider.Winapi, span, name, kind /*, CiItemAccess.Internal*/);
            items.Add(ci);
        }

        return(new() { _typenameStart = typenameStart, _canInsert = typeSym.IsFromSource() });

        //Now (May 2021) there are ~51000 items, and uses ~16 MB of memory while showing the popup list.
        //	Without CompletionItem would be ~8 MB. But now it is used everywhere and need some work to remove. Never mind.
    }
Ejemplo n.º 2
0
    void _Sort()
    {
        _aVisible.Sort((i1, i2) => {
            CiComplItem c1 = _a[i1], c2 = _a[i2];

            int diff = c1.moveDown - c2.moveDown;
            if (diff != 0)
            {
                return(diff);
            }

            if (_groupsEnabled)
            {
                diff = c1.group - c2.group;
                if (diff != 0)
                {
                    return(diff);
                }
                if (_groups[c1.group].NE())
                {
                    diff = c1.kind - c2.kind;
                    if (diff != 0)
                    {
                        return(diff);
                    }
                }
            }

            return(i1 - i2);            //the list is already sorted by name
        });
    }
Ejemplo n.º 3
0
    public static void Commit(SciCode doc, CiComplItem item, int codeLenDiff)
    {
        var    snippet = item as _CiComplItemSnippet;
        string s, usingDir;
        var    ci = item.ci;
        int    pos = ci.Span.Start, endPos = pos + ci.Span.Length + codeLenDiff;

        //list of snippets?
        var x = snippet.x;

        if (x.HasElements)
        {
            var a = snippet.x.Elements("list").ToArray();
            var m = new popupMenu();
            foreach (var v in a)
            {
                m.Add(v.Attr("item"));
            }
            m.FocusedItem = m.Items.First();
            int g = m.Show(MSFlags.ByCaret | MSFlags.Underline);
            if (g == 0)
            {
                return;
            }
            x = a[g - 1];
        }
        s = x.Value;

        //##directive -> #directive
        if (s.Starts('#') && doc.zText.Eq(pos - 1, '#'))
        {
            s = s[1..];
Ejemplo n.º 4
0
    public static string GetDescriptionHtml(CiComplItem item)
    {
        var snippet = item as _CiComplItemSnippet;
        var b       = new StringBuilder("<body><div>Snippet <b>");

        b.Append(item.DisplayText).Append("</b>.");
        _AppendInfo(snippet.x);
        bool isList = snippet.x.HasElements;

        if (isList)
        {
            foreach (var v in snippet.x.Elements("list"))
            {
                b.Append("<hr><div>").Append(v.Attr("item"));
                _AppendInfo(v);
                _AppendCode(v);
            }
        }
        else
        {
            _AppendCode(snippet.x);
        }
        if (snippet.x.Attr(out string more, "more"))
        {
            if (isList)
            {
                b.Append("<hr>");
            }
            b.Append("<p>").Append(more).Append("</p>");
        }
        b.Append("</body>");
        return(b.ToString());

        void _AppendInfo(XElement x)
        {
            if (x.Attr(out string info, "info"))
            {
                b.Append(' ').Append(info);
            }
            b.Append("</div>");
        }

        void _AppendCode(XElement x)
        {
            var s = x.Value;

            s = s.Replace("$end$", "");
            s = WebUtility.HtmlEncode(s);
            b.Append("<code><pre>").Append(s);
            if (!s.Ends('\n'))
            {
                b.AppendLine();
            }
            b.Append("</pre></code>");
        }
    }
Ejemplo n.º 5
0
 public void OnCommitInsertDeclaration(CiComplItem item)
 {
     if (!_GetText(item, out string text))
     {
         return;
     }
     if (_InsertDeclaration(item, text))
     {
         return;
     }
     clipboard.text = text;
     print.it("<>Clipboard:\r\n<code>" + text + "</code>");
 }
Ejemplo n.º 6
0
    public static System.Windows.Documents.Section GetDescription(CiComplItem item)
    {
        var m = new CiText();

        m.StartParagraph();
        m.Append(item.kind.ToString() + " "); m.Bold(item.Text); m.Append(".");
        m.EndParagraph();
        if (_GetText(item, out string s))
        {
            m.CodeBlock(s);
        }
        return(m.Result);
    }
Ejemplo n.º 7
0
    public static System.Windows.Documents.Section GetDescription(CiComplItem item)
    {
        var snippet = item as _CiComplItemSnippet;
        var m       = new CiText();

        m.StartParagraph();
        m.Append("Snippet "); m.Bold(item.Text); m.Append(".");
        _AppendInfo(snippet.x);
        bool isList = snippet.x.HasElements;

        if (isList)
        {
            foreach (var v in snippet.x.Elements("list"))
            {
                m.Separator();
                m.StartParagraph();
                m.Append(StringUtil.RemoveUnderlineChar(v.Attr("item")));
                _AppendInfo(v);
                _AppendCode(v);
            }
        }
        else
        {
            _AppendCode(snippet.x);
        }
        if (snippet.x.Attr(out string more, "more"))
        {
            if (isList)
            {
                m.Separator();
            }
            m.StartParagraph(); m.Append(more); m.EndParagraph();
        }
        return(m.Result);

        void _AppendInfo(XElement x)
        {
            if (x.Attr(out string info, "info"))
            {
                m.Append(" " + info);
            }
            m.EndParagraph();
        }

        void _AppendCode(XElement x)
        {
            m.CodeBlock(x.Value.Replace("$end$", ""));
        }
    }
Ejemplo n.º 8
0
 public static int Compare(CiComplItem i1, CiComplItem i2)
 {
     if (i1 is _CiComplItemSnippet s1 && i2 is _CiComplItemSnippet s2)
     {
         if (!s1.custom)
         {
             return(1);
         }
         else if (!s2.custom)
         {
             return(-1);                                                       //sort custom first
         }
     }
     return(0);
 }
Ejemplo n.º 9
0
    ///// <summary>
    ///// The child list control.
    ///// </summary>
    //public KTreeView Control => _tv;

    public CiPopupList(CiCompletion compl)
    {
        _compl = compl;

        _tv = new KTreeView {
            ItemMarginLeft = 20,
            //HotTrack = true, //no
            CustomDraw = new _CustomDraw(this),
            //test = true
        };
        _tv.ItemActivated  += _tv_ItemActivated;
        _tv.SelectedSingle += _tv_SelectedSingle;

        _tb = new StackPanel {
            Background = SystemColors.ControlBrush
        };

        var cstyle = Application.Current.FindResource(ToolBar.CheckBoxStyleKey) as Style;
        var bstyle = Application.Current.FindResource(ToolBar.ButtonStyleKey) as Style;

        var kindNames = CiUtil.ItemKindNames;

        _kindButtons = new CheckBox[kindNames.Length];
        for (int i = 0; i < kindNames.Length; i++)
        {
            _AddButton(_kindButtons[i] = new CheckBox(), kindNames[i], CiComplItem.ImageResource((CiItemKind)i));
            _kindButtons[i].Click     += _KindButton_Click;
        }

        _tb.Children.Add(new Separator());

        _AddButton(_groupButton = new CheckBox(), "Group by namespace or inheritance", "resources/ci/groupby.xaml");
        _groupButton.Click     += _GroupButton_Click;
        if (App.Settings.ci_complGroup)
        {
            _groupButton.IsChecked = true;
        }

        //var options = new Button();
        //options.Click += _Options_Click;
        //_AddButton(options, "Options", "resources/images/settingsgroup_16x.xaml");

        void _AddButton(ButtonBase b, string text, string image)
        {
            b.Style   = (b is CheckBox) ? cstyle : bstyle;
            b.Content = ResourceUtil.GetWpfImageElement(image);
            b.ToolTip = text;
            AutomationProperties.SetName(b, text);
            b.Focusable = false;             //would close popup
            _tb.Children.Add(b);
        }

        _panel = new DockPanel {
            Background = SystemColors.WindowBrush
        };
        DockPanel.SetDock(_tb, Dock.Left);
        _panel.Children.Add(_tb);
        _panel.Children.Add(_tv);
        _popup = new KPopup {
            Size       = (300, 360),
            Content    = _panel,
            CloseHides = true,
            Name       = "Ci.Completion",
            WindowName = "Au completion list",
        };

        _textPopup = new CiPopupText(CiPopupText.UsedBy.PopupList);
        _tpTimer   = new timer(_ShowTextPopup);
    }
Ejemplo n.º 10
0
    void _SortAndSetControlItems()
    {
        _av.Sort((c1, c2) => {
            int diff = c1.moveDown - c2.moveDown;
            if (diff != 0)
            {
                return(diff);
            }

            if (_groupsEnabled)
            {
                diff = c1.group - c2.group;
                if (diff != 0)
                {
                    return(diff);
                }
                if (_groups[c1.group].NE())
                {
                    CiItemKind k1 = c1.kind, k2 = c2.kind;
                    //let EnumMember be by Enum. In CiItemKind they are not adjacent, it would be not good.
                    if (k1 > CiItemKind.Enum)
                    {
                        if (k1 == CiItemKind.EnumMember)
                        {
                            k1 = CiItemKind.Enum + 1;
                        }
                        else
                        {
                            k1++;
                        }
                    }
                    if (k2 > CiItemKind.Enum)
                    {
                        if (k2 == CiItemKind.EnumMember)
                        {
                            k2 = CiItemKind.Enum + 1;
                        }
                        else
                        {
                            k2++;
                        }
                    }
                    diff = k1 - k2;
                    if (diff != 0)
                    {
                        return(diff);
                    }
                }
            }

            int r = string.Compare(c1.ci.SortText, c2.ci.SortText, StringComparison.OrdinalIgnoreCase);
            if (r == 0)
            {
                r = CiSnippets.Compare(c1, c2);
                //if (r == 0) r = string.Compare(c1.Text, c2.Text, StringComparison.OrdinalIgnoreCase);
            }
            return(r);
        });

        CiComplItem prev = null;

        foreach (var v in _av)
        {
            var group = _groupsEnabled && (prev == null || v.group != prev.group) ? _groups[v.group] : null;
            v.SetDisplayText(group);
            prev = v;
        }

        _tv.SetItems(_av);
    }
Ejemplo n.º 11
0
    bool _InsertDeclaration(CiComplItem item, string text)
    {
        if (!_canInsert)
        {
            return(false);
        }
        if (!CodeInfo.GetDocumentAndFindNode(out var cd, out var typenameNode, _typenameStart))
        {
            return(false);
        }
        var semo = cd.semanticModel;
        var sym  = semo.GetSymbolInfo(typenameNode).Symbol;

        if (sym is not INamedTypeSymbol t || !t.IsFromSource())
        {
            return(false);
        }
        var sr = t.DeclaringSyntaxReferences[0];

        SciCode  doc     = cd.sci;
        FileNode fSelect = null;

        if (sr.SyntaxTree != semo.SyntaxTree)
        {
            var f = App.Model.Find(sr.SyntaxTree.FilePath, FNFind.CodeFile);
            if (!App.Model.SetCurrentFile(f, dontChangeTreeSelection: true))
            {
                return(false);
            }
            doc     = Panels.Editor.ZActiveDoc;
            fSelect = cd.sci.ZFile;
        }

        var hs = new HashSet <string>();

        using (new KScintilla.UndoAction(doc)) {
            _Insert(0, sr.GetSyntax(), text, item.Text, item.kind);
        }

        if (fSelect != null)
        {
            App.Model.SetCurrentFile(fSelect);
        }
        return(true);

        void _Insert(int level, SyntaxNode node, string text, string name, CiItemKind kind)
        {
            if (node is not ClassDeclarationSyntax nodeCD)
            {
                return;
            }
            int    posClass = nodeCD.Keyword.SpanStart, posInsert = nodeCD.CloseBraceToken.SpanStart;
            string emptyLine = "\r\n";

            //if constant, try to insert below the last existing constant with same prefix
            if (kind == CiItemKind.Constant)
            {
                int u = name.IndexOf('_') + 1;
                if (u > 1)
                {
                    string prefix = " " + name[..u], code = doc.zText;
Ejemplo n.º 12
0
 static bool _GetText(CiComplItem item, out string text)
 {
     using var db = EdDatabases.OpenWinapi();
     return(db.Get(out text, $"SELECT code FROM api WHERE name='{item.Text}'"));
 }
Ejemplo n.º 13
0
    public static CompletionChange GetCompletionChange(SciCode doc, CiComplItem item, out int selectLength, out bool showSignature, out string usingDir)
    {
        selectLength = 0; showSignature = false; usingDir = null;
        var    snippet = item as _CiComplItemSnippet;
        string s;
        var    ci  = item.ci;
        int    pos = ci.Span.Start;

        //if helpSnippet above method, add parameters
        if (snippet.DisplayText == "helpSnippet")
        {
            s = snippet.x.Value;
            int j = s.Find("$signature$");
            if (j >= 0)
            {
                string sig = null;
                if (CodeInfo.GetContextAndDocument(out var cd, pos))
                {
                    var code2 = cd.code.RegexReplace(@"\w+\s+", "", 1, RXFlags.ANCHORED, pos..);
                    var doc2  = cd.document.WithText(SourceText.From(code2));
                    var node  = doc2.GetSyntaxRootAsync().Result.FindToken(pos).Parent;
                    for (; node != null && node.Span.Start >= pos; node = node.Parent)
                    {
                        if (node is MethodDeclarationSyntax md)
                        {
                            var b = new StringBuilder();
                            foreach (var p in md.ParameterList.Parameters)
                            {
                                b.Append("\r\n/// <param name=\"").Append(p.Identifier.Text).Append("\"></param>");
                            }
                            var rt = md.ReturnType;
                            if (!code2.Eq(rt.Span.Start..rt.Span.End, "void"))
                            {
                                b.Append("\r\n/// <returns></returns>");
                            }
                            sig = b.ToString();
                            break;
                        }
                    }
                }
                s = s.ReplaceAt(j, 11, sig);
            }
        }
        else
        {
            //list of snippets?
            var x = snippet.x;
            if (x.HasElements)
            {
                x = null;
                var m = new AMenu {
                    Modal = true
                };
                foreach (var v in snippet.x.Elements("list"))
                {
                    m[v.Attr("item")] = o => x = v;
                }
                m.Control.Items[0].Select();
                m.Show(doc, byCaret: true);
                if (x == null)
                {
                    return(null);
                }
            }
            s = x.Value;

            //##directive -> #directive
            if (s.Starts('#') && doc.Text.Eq(pos - 1, '#'))
            {
                s = s[1..];