Пример #1
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)> SuggestionsForAmbiguousIdentifiers
            (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(NonNullable <string> suggestedNS, string id, Position pos)
            {
                var edit = new TextEdit {
                    Range = new LSP.Range {
                        Start = pos, End = pos
                    }, NewText = $"{suggestedNS.Value}."
                };

                return($"{suggestedNS.Value}.{id}", file.GetWorkspaceEdit(edit));
            }

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

            return(suggestedIdQualifications.Concat(suggestedTypeQualifications));
        }
Пример #2
0
        /// <summary>
        /// Returns a sequence of suggestions on how errors for unknown types and callable in the given diagnostics can be fixed,
        /// given the file for which those diagnostics were generated and the corresponding compilation.
        /// The given line number is used to determine the containing namespace.
        /// Returns an empty enumerable if any of the given arguments is null.
        /// </summary>
        internal static IEnumerable <(string, WorkspaceEdit)> SuggestionsForUnknownIdentifiers
            (this FileContentManager file, CompilationUnit compilation, int lineNr, IEnumerable <Diagnostic> diagnostics)
        {
            if (file == null || diagnostics == null)
            {
                return(Enumerable.Empty <(string, WorkspaceEdit)>());
            }
            var unknownCallables = diagnostics.Where(DiagnosticTools.ErrorType(ErrorCode.UnknownIdentifier));
            var unknownTypes     = diagnostics.Where(DiagnosticTools.ErrorType(ErrorCode.UnknownType));

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

            var suggestionsForIds = unknownCallables.Select(d => d.Range.Start)
                                    .SelectMany(pos => file.NamespaceSuggestionsForIdAtPosition(pos, compilation, out var _));
            var suggestionsForTypes = unknownTypes.Select(d => d.Range.Start)
                                      .SelectMany(pos => file.NamespaceSuggestionsForTypeAtPosition(pos, compilation, out var _));

            return(file.OpenDirectiveSuggestions(lineNr, suggestionsForIds.Concat(suggestionsForTypes).ToArray())
                   .Select(edit => (edit.NewText.Trim().Trim(';'), file.GetWorkspaceEdit(edit))));
        }