public CompletionContext(IDocumentAnalysis analysis, SourceLocation location, CompletionItemSource itemSource) { Location = location; Analysis = analysis; Position = Ast.LocationToIndex(location); ItemSource = itemSource; }
private static IEnumerable <CompletionItem> GetAllImportableModules(CompletionContext context) { var mres = context.Analysis.Document.Interpreter.ModuleResolution; var modules = mres.CurrentPathResolver.GetAllModuleNames().Distinct(); return(modules.Select(n => CompletionItemSource.CreateCompletionItem(n, CompletionItemKind.Module))); }
public CompletionContext(IDocumentAnalysis analysis, SourceLocation location, CompletionItemSource itemSource, IServiceContainer services) { Location = location; Analysis = analysis; Position = Ast.LocationToIndex(location); ItemSource = itemSource; Services = services; }
private static IEnumerable <CompletionItem> GetAllImportableModules(CompletionContext context) { var interpreter = context.Analysis.Document.Interpreter; var languageVersion = interpreter.LanguageVersion.ToVersion(); var includeImplicit = !ModulePath.PythonVersionRequiresInitPyFiles(languageVersion); var modules = interpreter.ModuleResolution.CurrentPathResolver.GetAllImportableModuleNames(includeImplicit); return(modules .Where(n => !string.IsNullOrEmpty(n)) .Distinct() .Select(n => CompletionItemSource.CreateCompletionItem(n, CompletionItemKind.Module))); }
private static IReadOnlyList <CompletionItem> GetChildModules(string[] names, CompletionContext context) { if (!names.Any()) { return(Array.Empty <CompletionItem>()); } var mres = context.Analysis.Document.Interpreter.ModuleResolution; var fullName = string.Join(".", names.Take(names.Length - 1)); var import = mres.CurrentPathResolver.GetModuleImportFromModuleName(fullName); if (string.IsNullOrEmpty(import?.ModulePath)) { return(Array.Empty <CompletionItem>()); } var packages = mres.GetPackagesFromDirectory(Path.GetDirectoryName(import.ModulePath)); return(packages.Select(n => CompletionItemSource.CreateCompletionItem(n, CompletionItemKind.Module)).ToArray()); }
private static IEnumerable <CompletionItem> GetKeywordItems(CompletionContext context, CompletionListOptions options, ScopeStatement scope) { var keywords = Enumerable.Empty <string>(); if ((options & CompletionListOptions.ExpressionKeywords) == CompletionListOptions.ExpressionKeywords) { // keywords available in any context keywords = PythonKeywords.Expression(context.Ast.LanguageVersion); } if ((options & CompletionListOptions.StatementKeywords) == CompletionListOptions.StatementKeywords) { keywords = keywords.Union(PythonKeywords.Statement(context.Ast.LanguageVersion)); } if (!(scope is FunctionDefinition)) { keywords = keywords.Except(PythonKeywords.InvalidOutsideFunction(context.Ast.LanguageVersion)); } return(keywords.Select(kw => CompletionItemSource.CreateCompletionItem(kw, CompletionItemKind.Keyword))); }
public CompletionSource(IDocumentationSource docSource, ServerSettings.PythonCompletionOptions completionSettings, IServiceContainer services) { _itemSource = new CompletionItemSource(docSource, completionSettings); _services = services; }
private static CompletionResult GetResultFromImportSearch(IImportSearchResult importSearchResult, CompletionContext context, bool prependStar, SourceSpan?applicableSpan = null) { var document = context.Analysis.Document; var mres = document.Interpreter.ModuleResolution; IPythonModule module; switch (importSearchResult) { case ModuleImport moduleImports: module = mres.GetImportedModule(moduleImports.FullName); break; case PossibleModuleImport possibleModuleImport: module = mres.GetImportedModule(possibleModuleImport.PossibleModuleFullName); break; case ImplicitPackageImport _: module = null; break; default: return(CompletionResult.Empty); } var completions = new List <CompletionItem>(); if (prependStar) { completions.Add(CompletionItemSource.Star); } if (module != null) { completions.AddRange(module.GetMemberNames() .Where(n => !string.IsNullOrEmpty(n)) .Select(n => context.ItemSource.CreateCompletionItem(n, module.GetMember(n)))); } if (importSearchResult is IImportChildrenSource children) { foreach (var childName in children.GetChildrenNames()) { if (!children.TryGetChildImport(childName, out var imports)) { continue; } switch (imports) { case ImplicitPackageImport packageImport: completions.Add(CompletionItemSource.CreateCompletionItem(packageImport.Name, CompletionItemKind.Module)); break; case ModuleImport moduleImport when !moduleImport.ModulePath.PathEquals(document.FilePath): completions.Add(CompletionItemSource.CreateCompletionItem(moduleImport.Name, CompletionItemKind.Module)); break; } } } return(new CompletionResult(completions, applicableSpan)); }
public static CompletionResult GetCompletions(Node statement, ScopeStatement scopeStatement, CompletionContext context) { SourceSpan?applicableSpan = null; var eval = context.Analysis.ExpressionEvaluator; var options = GetOptions(statement, context.Position, out var span); if (span.HasValue) { applicableSpan = new SourceSpan(context.IndexToLocation(span.Value.Start), context.IndexToLocation(span.Value.End)); } var scope = context.Analysis.FindScope(context.Location); IEnumerable <CompletionItem> items; using (eval.OpenScope(scope)) { // Get variables declared in the module. var variables = eval.CurrentScope.EnumerateTowardsGlobal.SelectMany(s => s.Variables).ToArray(); items = variables.Select(v => context.ItemSource.CreateCompletionItem(v.Name, v)).ToArray(); } // Get builtins var builtins = context.Analysis.Document.Interpreter.ModuleResolution.BuiltinsModule; var builtinItems = builtins.GetMemberNames() .Select(n => { var m = builtins.GetMember(n); if ((options & CompletionListOptions.ExceptionsOnly) == CompletionListOptions.ExceptionsOnly && !IsExceptionType(m.GetPythonType())) { return(null); } return(context.ItemSource.CreateCompletionItem(n, m)); }).ExcludeDefault(); items = items.Concat(builtinItems); // Add possible function arguments. var finder = new ExpressionFinder(context.Ast, new FindExpressionOptions { Calls = true }); if (finder.GetExpression(context.Position) is CallExpression callExpr && callExpr.GetArgumentAtIndex(context.Ast, context.Position, out _)) { var value = eval.GetValueFromExpression(callExpr.Target); if (value?.GetPythonType() is IPythonFunctionType ft) { var arguments = ft.Overloads.SelectMany(o => o.Parameters).Select(p => p?.Name) .Where(n => !string.IsNullOrEmpty(n)) .Distinct() .Except(callExpr.Args.MaybeEnumerate().Select(a => a.Name).Where(n => !string.IsNullOrEmpty(n))) .Select(n => CompletionItemSource.CreateCompletionItem($"{n}=", CompletionItemKind.Variable)) .ToArray(); items = items.Concat(arguments).ToArray(); } } var keywords = GetKeywordItems(context, options, scopeStatement); items = items.Concat(keywords); return(new CompletionResult(items, applicableSpan)); }
public CompletionSource(IDocumentationSource docSource, ServerSettings.PythonCompletionOptions completionSettings) { _itemSource = new CompletionItemSource(docSource, completionSettings); }