Beispiel #1
0
        internal void AddNamespaces(XCompletionList compList, XSharpSearchLocation location, string startWith)
        {
            // We are looking for NameSpaces, in References
            if (startWith == null)
            {
                return;
            }
            var namespaces = location.Project.AllNamespaces;
            // Calculate the length we must remove
            int startLen = 0;
            int dotPos   = startWith.LastIndexOf('.');

            if (dotPos != -1)
            {
                startLen = dotPos + 1;
            }
            ImageSource icon      = _glyphService.GetGlyph(StandardGlyphGroup.GlyphGroupNamespace, StandardGlyphItem.GlyphItemPublic);
            ImageSource iconClass = _glyphService.GetGlyph(StandardGlyphGroup.GlyphGroupClass, StandardGlyphItem.GlyphItemPublic);

            foreach (string nameSpace in namespaces.Where(ns => nameStartsWith(ns, startWith)))
            {
                string displayName = nameSpace;
                // remove the start
                if (startLen > 0)
                {
                    displayName = displayName.Substring(startLen);
                }
                // Do we have another part
                dotPos = displayName.IndexOf('.');
                // Then remove it
                if (dotPos > 0)
                {
                    displayName = displayName.Substring(0, dotPos);
                }
                //
                XSCompletion item;
                if (!compList.ContainsKey(displayName))
                {
                    if (displayName.IndexOf("<") > 0)
                    {
                        item = new XSCompletion(displayName, displayName, nameSpace, iconClass, null, Kind.Class, "");
                    }
                    else
                    {
                        item = new XSCompletion(displayName, displayName, "Namespace " + nameSpace, icon, null, Kind.Namespace, "");
                    }
                    if (!compList.Add(item))
                    {
                        break;
                    }
                }
            }
            //
            // And our own Namespaces
            AddXSharpNamespaces(compList, location, startWith, icon);
            // We should also add the external NameSpaces
            var prjs = location.Project.ReferencedProjects;

            foreach (var prj in prjs)
            {
                AddXSharpNamespaces(compList, location, startWith, icon);
            }
        }
        bool CompleteCompletionSession(char ch)
        {
            if (_completionSession == null)
            {
                return(false);
            }
            bool         commit       = false;
            bool         moveBack     = false;
            Completion   completion   = null;
            XSCompletion xscompletion = null;
            ITextCaret   caret        = null;

            WriteOutputMessage("CompleteCompletionSession()");
            if (_completionSession.SelectedCompletionSet != null)
            {
                bool addDelim = false;

                if (_completionSession.SelectedCompletionSet.SelectionStatus.Completion != null)
                {
                    completion = _completionSession.SelectedCompletionSet.SelectionStatus.Completion;
                }
                xscompletion = completion as XSCompletion;
                Kind kind = Kind.Unknown;
                if (xscompletion != null)
                {
                    kind = xscompletion.Kind;
                }
                bool ctor = false;
                // some tokens need to be added to the insertion text.
                switch (kind)
                {
                case Kind.Keyword:
                    formatKeyword(completion);
                    break;

                case Kind.Class:
                case Kind.Structure:
                case Kind.Constructor:
                    ctor = true;
                    goto default;

                default:
                    switch (ch)
                    {
                    case '{' when ctor:
                    case '}' when ctor:
                        if (!completion.InsertionText.EndsWith("{"))
                        {
                            completion.InsertionText += "{";
                        }
                        break;

                    case '\t':        // Tab
                    case '\r':        // CR
                    case '\n':        // CR
                    case '.':         // DOT
                    case ':':         // COLON
                    case '\0':
                        break;

                    default:
                        var s = ch.ToString();
                        if (!completion.InsertionText.EndsWith(s))
                        {
                            completion.InsertionText += s;
                        }
                        break;
                    }
                    break;
                }
                if ((_completionSession.SelectedCompletionSet.Completions.Count > 0) && (_completionSession.SelectedCompletionSet.SelectionStatus.IsSelected))
                {
                    if (XSettings.EditorCompletionAutoPairs)
                    {
                        caret    = _completionSession.TextView.Caret;
                        addDelim = true;
                        WriteOutputMessage(" --> select " + completion.InsertionText);
                        if (kind == Kind.Constructor)
                        {
                            completion.InsertionText += "{";
                        }
                        else if (kind.HasParameters() && !completion.InsertionText.EndsWith("("))
                        {
                            completion.InsertionText += "(";
                        }
                    }
                    commit = true;
                }
                else
                {
                    if (completion != null)
                    {
                        // Push the completion char into the InsertionText if needed
                        if (!completion.InsertionText.EndsWith(ch.ToString()))
                        {
                            completion.InsertionText += ch;
                        }
                        if (XSettings.EditorCompletionAutoPairs)
                        {
                            caret    = _completionSession.TextView.Caret;
                            addDelim = true;
                        }
                    }
                    commit = true;
                }
                if (addDelim)
                {
                    if (completion.InsertionText.EndsWith("("))
                    {
                        moveBack = true;
                        completion.InsertionText += ")";
                    }
                    else if (completion.InsertionText.EndsWith("{"))
                    {
                        moveBack = true;
                        completion.InsertionText += "}";
                    }
                    else if (completion.InsertionText.EndsWith("["))
                    {
                        moveBack = true;
                        completion.InsertionText += "]";
                    }
                }
            }
            if (commit)
            {
                WriteOutputMessage(" --> Commit");
                var session = _completionSession;
                session.Properties.TryGetProperty(XsCompletionProperties.Type, out IXTypeSymbol type);
                string insertionText = completion.InsertionText;
                session.Properties.TryGetProperty(XsCompletionProperties.Char, out char triggerChar);
                if (ch == '.' || ch == ':')
                {
                    if (insertionText.IndexOfAny("({".ToCharArray()) == -1)
                    {
                        completion.InsertionText += ch;
                    }
                }
                _completionSession.Commit();
                if (moveBack && (caret != null))
                {
                    caret.MoveToPreviousCaretPosition();
                }
                // if a method or constructor was chosen, then trigger the signature help
                if (insertionText.Contains('(') || insertionText.Contains('{'))
                {
                    TriggerSignatureHelp(type, insertionText, triggerChar);
                }
                return(true);
            }

            WriteOutputMessage(" --> Dismiss");
            _completionSession.Dismiss();


            return(false);
        }