public virtual void PostWalk(FromImportStatement node) { }
public override void PostWalk(FromImportStatement node) { }
public RemovedFromImportStatement(FromImportStatement fromImport) { _fromImport = fromImport; }
public static CompletionResult GetCompletionsInFromImport(FromImportStatement fromImport, CompletionContext context) { // No more completions after '*', ever! if (fromImport.Names != null && fromImport.Names.Any(n => n?.Name == "*" && context.Position > n.EndIndex)) { return(CompletionResult.Empty); } var document = context.Analysis.Document; var mres = document.Interpreter.ModuleResolution; foreach (var(name, asName) in ZipLongest(fromImport.Names, fromImport.AsNames).Reverse()) { if (asName != null && context.Position >= asName.StartIndex) { return(CompletionResult.Empty); } if (name != null) { if (context.Position > name.EndIndex && name.EndIndex > name.StartIndex) { var applicableSpan = context.GetApplicableSpanFromLastToken(fromImport); return(new CompletionResult(Enumerable.Repeat(CompletionItemSource.AsKeyword, 1), applicableSpan)); } if (context.Position >= name.StartIndex) { var applicableSpan = name.GetSpan(context.Ast); var importSearchResult = mres.CurrentPathResolver.FindImports(document.FilePath, fromImport); return(GetResultFromImportSearch(importSearchResult, context, false, applicableSpan)); } } } if (fromImport.ImportIndex > fromImport.StartIndex && context.Position > fromImport.ImportIndex + 6) { var importSearchResult = mres.CurrentPathResolver.FindImports(document.FilePath, fromImport); var result = GetResultFromImportSearch(importSearchResult, context, true); if (result != CompletionResult.Empty) { return(result); } } if (fromImport.ImportIndex > 0 && context.Position >= fromImport.ImportIndex) { var applicableSpan = new SourceSpan( context.IndexToLocation(fromImport.ImportIndex), context.IndexToLocation(Math.Min(fromImport.ImportIndex + 6, fromImport.EndIndex)) ); return(new CompletionResult(Enumerable.Repeat(CompletionItemSource.ImportKeyword, 1), applicableSpan)); } if (context.Position > fromImport.Root.EndIndex && fromImport.Root.EndIndex > fromImport.Root.StartIndex) { SourceSpan?applicableSpan = null; if (context.Position > fromImport.EndIndex) { // Only end up here for "from ... imp", and "imp" is not counted // as part of our span var token = context.TokenSource.Tokens.LastOrDefault(); if (token.Key.End >= context.Position) { applicableSpan = context.TokenSource.GetTokenSpan(token.Key); } } return(new CompletionResult(Enumerable.Repeat(CompletionItemSource.ImportKeyword, 1), applicableSpan)); } if (context.Position > fromImport.Root.StartIndex && fromImport.Root is RelativeModuleName relativeName) { var rootNames = relativeName.Names.Select(n => n.Name); var importSearchResult = mres.CurrentPathResolver.GetImportsFromRelativePath(document.FilePath, relativeName.DotCount, rootNames); return(GetResultFromImportSearch(importSearchResult, context, false)); } if (fromImport.Root.Names.Count > 1 && context.Position > fromImport.Root.Names[0].EndIndex) { var rootNames = fromImport.Root.Names.TakeWhile(n => n.EndIndex < context.Position).Select(n => n.Name); var importSearchResult = mres.CurrentPathResolver.GetImportsFromAbsoluteName(document.FilePath, rootNames, fromImport.ForceAbsolute); return(GetResultFromImportSearch(importSearchResult, context, false)); } return(context.Position > fromImport.KeywordEndIndex ? new CompletionResult(GetAllImportableModules(context)) : null); }
public override void PostWalk(FromImportStatement node) { PostWalkWorker(node); }
public static IImportSearchResult FindImports(this PathResolverSnapshot pathResolver, string modulePath, FromImportStatement fromImportStatement) { var rootNames = fromImportStatement.Root.Names.Select(n => n.Name); return(fromImportStatement.Root is RelativeModuleName relativeName ? pathResolver.GetImportsFromRelativePath(modulePath, relativeName.DotCount, rootNames) : pathResolver.GetImportsFromAbsoluteName(modulePath, rootNames, fromImportStatement.ForceAbsolute)); }
private List <Inferred> InferFromImportStatement(FromImportStatement node, Scope scope) { Debug.Print("Not implemented: InferFromImportStatementIn"); return(null); }
/// <summary> /// Extracts actual module names from the 'from import' statement /// such as [a, b, c] from 'from . import a, b, c' or [a] from 'from .a import b, c' /// </summary> /// <param name="importingFromModuleName">Name of the importing module</param> /// <param name="importingFromFilePath">Disk path to the importing module</param> /// <param name="node">Import statement node</param> /// <returns></returns> internal static IEnumerable <string> GetModuleNamesFromImport(string importingFromModuleName, string importingFromFilePath, FromImportStatement node) { var root = node.Root.MakeString(); if (!string.IsNullOrEmpty(root) && root.StartsWithOrdinal(".")) { return(root.All(c => c == '.') ? node.Names.Where(n => !string.IsNullOrEmpty(n.Name)).Select(n => n.Name) : ResolvePotentialModuleNames(importingFromModuleName, importingFromFilePath, root, node.ForceAbsolute)); } return(new[] { root }); }
public override bool Walk(FromImportStatement node) { var modName = node.Root.MakeString(); if (!TryImportModule(modName, node.ForceAbsolute, out var modRef, out var bits)) { _unit.DeclaringModule.AddUnresolvedModule(modName, node.ForceAbsolute); return(false); } Debug.Assert(modRef.Module != null); modRef.Module.Imported(_unit); string[] fullImpName; if (bits != null) { fullImpName = new string[bits.Count + 1]; bits.ToArray().CopyTo(fullImpName, 0); } else { fullImpName = new string[1]; } var asNames = node.AsNames ?? node.Names; int len = Math.Min(node.Names.Count, asNames.Count); for (int i = 0; i < len; i++) { var impName = node.Names[i].Name; if (string.IsNullOrEmpty(impName)) { // incomplete import statement continue; } else if (impName == "*") { // Handle "import *" foreach (var varName in modRef.Module.GetModuleMemberNames(GlobalScope.InterpreterContext)) { if (!varName.StartsWithOrdinal("_")) { fullImpName[fullImpName.Length - 1] = varName; // Don't add references to "*" node FinishImportModuleOrMember(modRef, fullImpName, varName, false, node.Names[i], null); } } } else { fullImpName[fullImpName.Length - 1] = impName; var varName = asNames[i]?.Name ?? impName; FinishImportModuleOrMember(modRef, fullImpName, varName, true, node.Names[i], asNames[i]); } } return(false); }
public RemovedFromImportStatement(FromImportStatement fromImport, PythonAst ast) { _fromImport = fromImport; LeadingWhitespace = fromImport.GetPreceedingWhiteSpaceDefaultNull(ast); }
public override bool Walk(FromImportStatement node) { var modName = node.Root.MakeString(); if (!TryImportModule(modName, node.ForceAbsolute, out var modRef, out var bits)) { _unit.DeclaringModule.AddUnresolvedModule(modName, node.ForceAbsolute); return(false); } _unit.DeclaringModule.AddModuleReference(modRef); Debug.Assert(modRef.Module != null); var userMod = modRef.Module; string[] fullImpName; if (bits != null) { fullImpName = new string[bits.Count + 1]; bits.ToArray().CopyTo(fullImpName, 0); } else { fullImpName = new string[1]; } var asNames = node.AsNames ?? node.Names; int len = Math.Min(node.Names.Count, asNames.Count); for (int i = 0; i < len; i++) { var nameNode = asNames[i] ?? node.Names[i]; var impName = node.Names[i].Name; var newName = asNames[i] != null ? asNames[i].Name : null; if (string.IsNullOrEmpty(impName)) { // incomplete import statement continue; } else if (impName == "*") { // Handle "import *" if (userMod != null) { userMod.Imported(_unit); foreach (var varName in userMod.GetModuleMemberNames(GlobalScope.InterpreterContext)) { if (!varName.StartsWithOrdinal("_")) { fullImpName[fullImpName.Length - 1] = varName; AssignImportedMember(nameNode, userMod, fullImpName, varName); } } } } else { userMod.Imported(_unit); fullImpName[fullImpName.Length - 1] = impName; AssignImportedMember(nameNode, userMod, fullImpName, newName ?? impName); } } return(false); }
public override bool Walk(FromImportStatement node) { var modName = node.Root.MakeString(); if (modName == "__future__") { return(false); } if (node.Names == null) { return(false); } bool onlyImportModules = modName.EndsWith("."); var mod = new AstNestedPythonModule( _interpreter, modName, PythonAnalyzer.ResolvePotentialModuleNames(_module.Name, _filePath, modName, true).ToArray() ); var ctxt = _interpreter.CreateModuleContext(); mod.Imported(ctxt); // Ensure child modules have been loaded mod.GetChildrenModules(); try { for (int i = 0; i < node.Names.Count; ++i) { if (!onlyImportModules) { if (node.Names[i].Name == "*") { foreach (var member in mod.GetMemberNames(ctxt)) { var mem = mod.GetMember(ctxt, member) ?? new AstPythonConstant( _interpreter.GetBuiltinType(BuiltinTypeId.Unknown), mod.Locations.ToArray() ); _scope.SetInScope(member, mem); (mem as IPythonModule)?.Imported(ctxt); } continue; } var n = node.AsNames?[i] ?? node.Names[i]; if (n != null) { var mem = mod.GetMember(ctxt, node.Names[i].Name) ?? new AstPythonConstant( _interpreter.GetBuiltinType(BuiltinTypeId.Unknown), GetLoc(n) ); _scope.SetInScope(n.Name, mem); (mem as IPythonModule)?.Imported(ctxt); } } } } catch (IndexOutOfRangeException) { } return(false); }
public override void PostWalk(FromImportStatement node) { Trace.Assert(!node.IsFromFuture); ImportedModules.Add(string.Join('.', node.Root.Names)); }
// FromImportStatement public virtual bool Walk(FromImportStatement node) { return true; }
public static void AddImport( IServiceProvider serviceProvider, ITextBuffer buffer, ITextView view, string fromModule, string name ) { var analyzer = buffer.GetAnalyzer(serviceProvider); var curAst = analyzer.ParseSnapshot(buffer.CurrentSnapshot); var suiteBody = curAst.Body as SuiteStatement; Statement insertBefore = null; if (suiteBody != null) { bool firstStatement = true; foreach (var statement in suiteBody.Statements) { if (firstStatement && IsDocString(statement as ExpressionStatement)) { // doc string, ignore this firstStatement = false; continue; } firstStatement = false; // __future__ imports go first if (fromModule == null || fromModule != "__future__") { if (statement is ImportStatement) { // we insert after this continue; } else if (statement is FromImportStatement) { // we might update this, we might insert after FromImportStatement fromImport = statement as FromImportStatement; if (fromImport.Root.MakeString() == fromModule) { // update the existing from ... import statement to include the new name. UpdateFromImport(curAst, buffer, fromImport, name); return; } continue; } } // this isn't an import, we insert before this statement insertBefore = statement; break; } } int start; if (insertBefore != null) { var location = insertBefore.GetStart(curAst); var point = buffer.CurrentSnapshot.GetLineFromLineNumber(location.Line - 1).Start; // the span starts after any whitespace, so walk backup and see if we should skip some lines if (point.Position != 0) { var prevPoint = point.Subtract(1); // walk past all the previous lines var classifier = buffer.GetPythonClassifier(); bool moved = false; while (prevPoint.Position != 0 && (char.IsWhiteSpace(prevPoint.GetChar()) || IsCommentChar(prevPoint, classifier))) { prevPoint = prevPoint.Subtract(1); moved = true; } prevPoint = prevPoint.Add(1); // then walk forward one line if (moved) { int lineNum = prevPoint.GetContainingLine().LineNumber; do { prevPoint = prevPoint.Add(1); } while (lineNum == prevPoint.GetContainingLine().LineNumber); } point = prevPoint; } start = point.Position; } else { start = 0; } buffer.Insert(start, MakeImportCode(fromModule, name) + view.Options.GetNewLineCharacter()); }
// FromImportStatement public override bool Walk(FromImportStatement node) { return false; }
public override bool Walk(FromImportStatement node) { var mods = ProjectState.GetReflectedNamespaces(node.Root.Names, true); ModuleReference moduleRef; Namespace userMod = null; var modName = node.Root.MakeString(); if (ProjectState.Modules.TryGetValue(modName, out moduleRef)) { userMod = moduleRef.Module; if (userMod == null) { moduleRef.References.Add(_unit); } } else { moduleRef = ProjectState.Modules[modName] = new ModuleReference(); if (moduleRef.References == null) { moduleRef.References = new HashSet <AnalysisUnit>(); } moduleRef.References.Add(_unit); } var asNames = node.AsNames ?? node.Names; var impInfo = new ImportInfo(node.Root.MakeString(), node.Span); GlobalScope.Imports[node] = impInfo; int len = Math.Min(node.Names.Count, asNames.Count); for (int i = 0; i < len; i++) { var impName = node.Names[i]; var newName = asNames[i]; if (impName == null) { // incomplete import statement continue; } else if (impName == "*") { // Handle "import *" if (userMod != null) { foreach (var varName in GetModuleKeys(userMod)) { WalkFromImportWorker(node, userMod, mods, varName, null); } } if (mods != null) { foreach (var mod in mods) { foreach (var name in Utils.DirHelper(mod, true)) { WalkFromImportWorker(node, null, mods, name, null); } } } } else { WalkFromImportWorker(node, userMod, mods, impName, newName); } } return(true); }
// FromImportStatement public override bool Walk(FromImportStatement node) { return(ShouldWalkWorker(node)); }
public override bool Walk(FromImportStatement node) { UpdateLineInfo(node); return(base.Walk(node)); }
public override bool Walk(FromImportStatement node) { AddTagIfNecessary(node); return(base.Walk(node)); }
public override Task <bool> WalkAsync(FromImportStatement node, CancellationToken cancellationToken = default) => FromImportHandler.HandleFromImportAsync(node, cancellationToken);
private Reference HandleFromImport(IDocumentAnalysis analysis, SourceLocation location, FromImportStatement statement, Node expr, out ILocatedMember definingMember) { definingMember = null; var mres = analysis.Document.Interpreter.ModuleResolution; var imports = mres.CurrentPathResolver.FindImports(analysis.Document.FilePath, statement); IPythonModule module = null; switch (imports) { case ModuleImport moduleImport: module = mres.GetImportedModule(moduleImport.FullName); break; case ImplicitPackageImport packageImport: module = mres.GetImportedModule(packageImport.FullName); break; } // Are we in the module name (i.e. A in 'from A import B')? var locationIndex = location.ToIndex(analysis.Ast); if (statement.Root.StartIndex <= locationIndex && locationIndex <= statement.Root.EndIndex) { definingMember = module; return(module != null ? new Reference { range = default, uri = CanNavigateToModule(module) ? module.Uri : null
// FromImportStatement public override bool Walk(FromImportStatement node) { return(Contains(node)); }
public UpdatedFromImportStatement(FromImportStatement fromImport) { _fromImport = fromImport; }
public override bool Walk(FromImportStatement node) { var modName = node.Root.MakeString(); if (modName == "__future__") { return(false); } if (node.Names == null) { return(false); } var mod = new AstNestedPythonModule( _interpreter, modName, PythonAnalyzer.ResolvePotentialModuleNames(_module.Name, _filePath, modName, true).ToArray() ); foreach (var name in GetImportNames(node.Names, node.AsNames)) { if (name.Key == "*") { mod.Imported(_scope.Context); // Ensure child modules have been loaded mod.GetChildrenModules(); foreach (var member in mod.GetMemberNames(_scope.Context)) { var mem = mod.GetMember(_scope.Context, member) ?? new AstPythonConstant(_unknownType, mod.Locations.ToArray()); if (mem.MemberType == PythonMemberType.Unknown && WarnAboutUndefinedValues) { _log?.Log(TraceLevel.Warning, "UndefinedImport", modName, name); } _scope.SetInScope(member, mem); (mem as IPythonModule)?.Imported(_scope.Context); } } else { IMember mem; if (mod.IsLoaded) { mem = mod.GetMember(_scope.Context, name.Key) ?? new AstPythonConstant(_unknownType, GetLoc(name.Value)); if (mem.MemberType == PythonMemberType.Unknown && WarnAboutUndefinedValues) { _log?.Log(TraceLevel.Warning, "UndefinedImport", modName, name); } (mem as IPythonModule)?.Imported(_scope.Context); } else { mem = new AstNestedPythonModuleMember(name.Key, mod, _scope.Context, GetLoc(name.Value)); } _scope.SetInScope(name.Value.Name, mem); } } return(false); }
// FromImportStatement public virtual bool Walk(FromImportStatement node) { return(true); }
// FromImportStatement public override bool Walk(FromImportStatement node) { return(Location >= node.StartIndex && Location <= node.EndIndex); }
// FromImportStatement public override bool Walk(FromImportStatement node) { return(false); }
public FromImportDefinition(FromImportStatement from) { this.from = from; }
// FromImportStatement public override bool Walk(FromImportStatement node) { return Location >= node.StartIndex && Location <= node.EndIndex; }
public override bool Walk(FromImportStatement node) => ImportHandler.HandleFromImport(node);
public override bool Walk(FromImportStatement node) { writer.WriteLine("FromImport: " + node.Root.MakeString()); return(base.Walk(node)); }
/// <summary> /// Extracts actual module names from the 'from import' statement /// such as [a, b, c] from 'from . import a, b, c' or [a] from 'from .a import b, c' /// </summary> /// <param name="entry">Project entry</param> /// <param name="node">Import statement node</param> /// <returns>Names of modules</returns> internal static IEnumerable <string> GetModuleNamesFromImport(IPythonProjectEntry entry, FromImportStatement node) => GetModuleNamesFromImport(entry.ModuleName, entry.FilePath, node);