示例#1
0
        /// <summary>
        /// Returns workspace edits for opening namespaces of unknown identifiers.
        /// </summary>
        private static IEnumerable <(string, WorkspaceEdit)> UnknownIdNamespaceSuggestions(
            this FileContentManager file,
            CompilationUnit compilation,
            int lineNr,
            IReadOnlyCollection <Diagnostic> diagnostics)
        {
            var idSuggestions = diagnostics
                                .Where(DiagnosticTools.ErrorType(ErrorCode.UnknownIdentifier))
                                .SelectMany(d => file.IdNamespaceSuggestions(d.Range.Start.ToQSharp(), compilation, out _));
            var typeSuggestions = diagnostics
                                  .Where(DiagnosticTools.ErrorType(ErrorCode.UnknownType))
                                  .SelectMany(d => file.TypeNamespaceSuggestions(d.Range.Start.ToQSharp(), compilation, out _));

            return(file
                   .OpenDirectiveSuggestions(lineNr, idSuggestions.Concat(typeSuggestions))
                   .Select(edit => (edit.NewText.Trim().Trim(';'), file.GetWorkspaceEdit(edit))));
        }
示例#2
0
        /// <summary>
        /// Returns a sequence of suggestions on how errors for ambiguous types and callable in the given diagnostics can be fixed,
        /// given the file for which those diagnostics were generated and the corresponding compilation.
        /// Returns an empty enumerable if any of the given arguments is null.
        /// </summary>
        internal static IEnumerable <(string, WorkspaceEdit)> AmbiguousIdSuggestions(
            this FileContentManager file, CompilationUnit compilation, IEnumerable <Diagnostic> diagnostics)
        {
            if (file == null || diagnostics == null)
            {
                return(Enumerable.Empty <(string, WorkspaceEdit)>());
            }
            var ambiguousCallables = diagnostics.Where(DiagnosticTools.ErrorType(ErrorCode.AmbiguousCallable));
            var ambiguousTypes     = diagnostics.Where(DiagnosticTools.ErrorType(ErrorCode.AmbiguousType));

            if (!ambiguousCallables.Any() && !ambiguousTypes.Any())
            {
                return(Enumerable.Empty <(string, WorkspaceEdit)>());
            }

            (string, WorkspaceEdit)? SuggestedNameQualification(string suggestedNS, string?id, Position pos)
            {
                var edit = new TextEdit
                {
                    Range = new Lsp.Range {
                        Start = pos.ToLsp(), End = pos.ToLsp()
                    },
                    NewText = $"{suggestedNS}."
                };

                return(id is null
                    ? null as (string, WorkspaceEdit)?
                    : ($"{suggestedNS}.{id}", file.GetWorkspaceEdit(edit)));
            }

            var suggestedIdQualifications = ambiguousCallables
                                            .Select(d => d.Range.Start.ToQSharp())
                                            .SelectMany(pos => file
                                                        .IdNamespaceSuggestions(pos, compilation, out var id)
                                                        .SelectNotNull(ns => SuggestedNameQualification(ns, id, pos)));
            var suggestedTypeQualifications = ambiguousTypes
                                              .Select(d => d.Range.Start.ToQSharp())
                                              .SelectMany(pos => file
                                                          .TypeNamespaceSuggestions(pos, compilation, out var id)
                                                          .SelectNotNull(ns => SuggestedNameQualification(ns, id, pos)));

            return(suggestedIdQualifications.Concat(suggestedTypeQualifications));
        }