public override bool Walk(FromImportStatement node) { if (node.StartIndex <= Location && Location <= node.EndIndex) { var nameNode = node.Names.MaybeEnumerate().Where(n => n.StartIndex <= Location && Location <= n.EndIndex).FirstOrDefault(); if (nameNode != null && node.AsNames != null) { var index = node.Names.IndexOf(nameNode); if (index < node.AsNames.Count && node.AsNames[index] != null) { ImportedType = GetNamedLocation(node.AsNames[index]); return(false); } } var modName = node.Root.MakeString(); // See if we can resolve relative names var candidates = ModuleResolver.ResolvePotentialModuleNames(_importingFromModuleName, _importingFromFilePath, modName, node.ForceAbsolute).ToArray(); if (candidates.Length == 1 && string.IsNullOrEmpty(candidates[0]) && node.Names != null && node.Names.Any()) { // Did not resolve to anything. Happens at the top of the workspace // in VS Code when using 'from . import a' ImportedModules = new[] { GetNamedLocation(node.Names.First()) }; } else if (candidates.Length > 0) { ImportedModules = candidates.Select(c => GetNamedLocation(c, node.Root)).ToArray(); } else { ImportedModules = new[] { GetNamedLocation(modName, node.Root) }; } } return(false); }
public override bool Walk(FromImportStatement node) { if (node.StartIndex <= Location && Location <= node.EndIndex) { // Determine if location is over imported parts such as // over 'a' in 'from . import a, b, c' or over 'x' in 'from a import x' // and store module names and imported parts ImportedModules = ModuleResolver.ResolvePotentialModuleNames(_importingFromModuleName, _importingFromFilePath, node.Root.MakeString(), node.ForceAbsolute); } return(false); }
private bool TryImportModule(string modName, bool forceAbsolute, out ModuleReference moduleRef, out IReadOnlyList <string> remainingParts) { moduleRef = null; remainingParts = null; if (ProjectState.Limits.CrossModule > 0 && ProjectState.ModulesByFilename.Count > ProjectState.Limits.CrossModule) { // too many modules loaded, disable cross module analysis by blocking // scripts from seeing other modules. return(false); } var candidates = ModuleResolver.ResolvePotentialModuleNames(_unit.ProjectEntry, modName, forceAbsolute).ToArray(); foreach (var name in candidates) { if (ProjectState.Modules.TryImport(name, out moduleRef)) { return(true); } } foreach (var name in candidates) { moduleRef = null; foreach (var part in ModulePath.GetParents(name, includeFullName: true)) { if (ProjectState.Modules.TryImport(part, out var mref)) { moduleRef = mref; if (part.Length < name.Length) { moduleRef.Module?.Imported(_unit); } } else if (moduleRef != null) { Debug.Assert(moduleRef.Name.Length + 1 < name.Length, $"Expected {name} to be a child of {moduleRef.Name}"); if (moduleRef.Name.Length + 1 < name.Length) { remainingParts = name.Substring(moduleRef.Name.Length + 1).Split('.'); } return(true); } else { break; } } } return(moduleRef?.Module != null); }
private bool TryImportModule(string modName, bool forceAbsolute, out ModuleReference moduleRef, out IReadOnlyList <string> remainingParts) { moduleRef = null; remainingParts = null; if (ProjectState.Limits.CrossModule > 0 && ProjectState.ModulesByFilename.Count > ProjectState.Limits.CrossModule) { // too many modules loaded, disable cross module analysis by blocking // scripts from seeing other modules. return(false); } var candidates = ModuleResolver.ResolvePotentialModuleNames(_unit.ProjectEntry, modName, forceAbsolute).ToArray(); foreach (var name in candidates) { ModuleReference modRef; bool gotAllParents = true; AnalysisValue lastParent = null; remainingParts = name.Split('.'); foreach (var part in ModulePath.GetParents(name, includeFullName: false)) { if (!ProjectState.Modules.TryImport(part, out modRef)) { gotAllParents = false; break; } moduleRef = modRef; (lastParent as BuiltinModule)?.AddChildModule(remainingParts[0], moduleRef.AnalysisModule); lastParent = moduleRef.AnalysisModule; remainingParts = remainingParts.Skip(1).ToArray(); } if (gotAllParents && ProjectState.Modules.TryImport(name, out modRef)) { moduleRef = modRef; (lastParent as BuiltinModule)?.AddChildModule(remainingParts[0], moduleRef.AnalysisModule); _unit.DeclaringModule.AddModuleReference(moduleRef); remainingParts = null; return(true); } } if (moduleRef?.Module != null) { _unit.DeclaringModule.AddModuleReference(moduleRef); return(true); } return(false); }
internal void AddUnresolvedModule(string relativeModuleName, bool absoluteImports) { _unresolvedModules.UnionWith(ModuleResolver.ResolvePotentialModuleNames(_projectEntry, relativeModuleName, absoluteImports)); _projectEntry.ProjectState.ModuleHasUnresolvedImports(this, true); }