示例#1
0
        private bool TryRegisterAutocompleteServer()
        {
            Object sessionRValueValue = GetSession();
            bool   returnValue        = false;

            if (sessionRValueValue != null && sessionRValueValue != sessionCache)
            {
                AutocompleteService autocompleteService = AutocompleteClient.GetAutocompleteService();
                if (autocompleteService != null)
                {
                    try
                    {
                        ((IClientChannel)autocompleteService).Close();
                    }
                    catch { }
                }
                sessionCache = sessionRValueValue;
                MethodInfo methodInfo = fsiAssembly.GetType("Microsoft.VisualStudio.FSharp.Interactive.Session+Session").GetMethod("get_Input");
                dynamic    fsiProcess = methodInfo.Invoke((Object)sessionRValueValue, null);

                fsiProcess.Invoke("printfn \"Registering Autocomplete provider\";;");
                fsiProcess.Invoke(String.Format("#r \"{0}\";;", typeof(AutocompleteServer).Assembly.Location));
                fsiProcess.Invoke(String.Format("FSharp.Interactive.Intellisense.Lib.AutocompleteServer.StartServer({0});;", sessionRValueValue.GetHashCode()));
                returnValue = true;

                System.Threading.Tasks.Task.Delay(4000).ContinueWith((t) =>
                {
                    // activate session
                    try
                    {
                        autocompleteService = AutocompleteClient.SetAutocompleteServiceChannel(sessionRValueValue.GetHashCode());
                        autocompleteService.Ping();
                        var comple = autocompleteService.GetCompletions("sys", IntellisenseProviderType.Combined);
                        fsiProcess.Invoke("printfn \"Autocomplete provider registration complete\";;");
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }
                });
            }
            else
            {
                AutocompleteService autocompleteService = AutocompleteClient.GetAutocompleteService();
                if (autocompleteService != null)
                {
                    try
                    {
                        autocompleteService.Ping();
                    }
                    catch { }
                }
            }

            return(returnValue);
        }
        void ICompletionSource.AugmentCompletionSession(ICompletionSession session, IList <CompletionSet> completionSets)
        {
            ITextSnapshot snapshot     = textBuffer.CurrentSnapshot;
            SnapshotPoint?triggerPoint = session.GetTriggerPoint(snapshot);

            if (triggerPoint == null)
            {
                return;
            }
            SnapshotPoint end   = triggerPoint.Value;
            SnapshotPoint start = end;

            // go back to either a delimiter, a whitespace char or start of line.
            while (start > 0)
            {
                SnapshotPoint prev = start - 1;
                if (IsWhiteSpaceOrDelimiter(prev.GetChar()))
                {
                    break;
                }
                start += -1;
            }

            var span = new SnapshotSpan(start, end);
            // The ApplicableTo span is what text will be replaced by the completion item
            ITrackingSpan applicableTo1 = snapshot.CreateTrackingSpan(span, SpanTrackingMode.EdgeInclusive);

            String statement = applicableTo1.GetText(applicableTo1.TextBuffer.CurrentSnapshot);

            AutocompleteService autocomplteService        = AutocompleteClient.GetAutocompleteService();
            IEnumerable <StatementCompletion> completions = new List <StatementCompletion>();

            if (autocomplteService != null)
            {
                try
                {
                    completions = autocomplteService.GetCompletions(statement, intellisenseProviderType);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                }
            }

            compList = new List <Completion>();
            bool prependDot = statement.EndsWith(".");

            foreach (StatementCompletion completion in completions)
            {
                //StatementCompletion completion = StatementCompletion.FromTuple(tuple.Item1, tuple.Item2);
                string str   = completion.Text;
                var    glyph = sourceProvider.GlyphService.GetGlyph(CompletionTypeToStandardGlyphGroup(completion.CompletionType),
                                                                    StandardGlyphItem.GlyphItemPublic);
                compList.Add(new Completion(str, prependDot ? "." + str : str, str, glyph, null));
            }

            var applicableTo = FindTokenSpanAtPosition(session.GetTriggerPoint(textBuffer),
                                                       session);

            CompletionSet completionSet = new CompletionSet(
                "F# completions",    //the non-localized title of the tab
                "F# completions",    //the display title of the tab
                applicableTo,
                compList,
                null);

            // Following code doesn't work:
            //ExposedObject.From(completionSet)._filterMatchType =
            //    Microsoft.VisualStudio.Language.Intellisense.CompletionMatchType.MatchInsertionText;

            completionSets.Add(completionSet);
        }