this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

internal/lsp: simplify processing of package imports

Now that we explicitly model imports within a Package (as well as the
inverted import graph "pkg.importedBy"), and definitions understands how
to find the canonical form of an import, we can can simplify how
Packages process imports.

Signed-off-by: Matthew Sackman <matthew@cue.works>
Change-Id: I62af343f9c32d63e5b6def486805f0652aead059
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1225184
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+24 -44
+13 -34
internal/lsp/cache/package.go
··· 181 181 fileUri := m.rootURI + protocol.DocumentURI("/"+file.FilePath) 182 182 w.standalone.reloadFile(fileUri) 183 183 } 184 - for _, importedModpkg := range modpkg.Imports() { 185 - if isUnhandledPackage(importedModpkg) { 186 - continue 187 - } 188 - ip := normalizeImportPath(importedModpkg) 189 - modRootURI := moduleRootURI(importedModpkg) 190 - if importedPkg, found := w.findPackage(modRootURI, ip); found { 191 - importedPkg.RemoveImportedBy(pkg) 192 - } 193 - } 184 + } 185 + for _, importedPkg := range pkg.imports { 186 + importedPkg.RemoveImportedBy(pkg) 194 187 } 188 + pkg.imports = nil 195 189 w.debugLogf("%v Deleted", pkg) 196 190 w.invalidateActiveFilesAndDirs() 197 191 } ··· 227 221 for _, file := range oldModpkg.Files() { 228 222 delete(w.mappers, file.Syntax.Pos().File()) 229 223 } 230 - for _, importedModpkg := range oldModpkg.Imports() { 231 - if isUnhandledPackage(importedModpkg) { 232 - continue 233 - } 234 - ip := normalizeImportPath(importedModpkg) 235 - modRootURI := moduleRootURI(importedModpkg) 236 - if importedPkg, found := w.findPackage(modRootURI, ip); found { 237 - importedPkg.RemoveImportedBy(pkg) 238 - } 239 - } 240 - pkg.imports = pkg.imports[:0] 224 + } 225 + 226 + for _, importedPkg := range pkg.imports { 227 + importedPkg.RemoveImportedBy(pkg) 241 228 } 229 + pkg.imports = pkg.imports[:0] 242 230 243 231 importCanonicalisation := make(map[string]ast.ImportPath) 244 232 importCanonicalisation[modpkg.ImportPath()] = pkg.importPath ··· 257 245 importCanonicalisation[ast.ParseImportPath(importedModpkg.ImportPath()).Canonical().String()] = importedPkg.importPath 258 246 } 259 247 } 248 + pkg.imports = slices.Clip(pkg.imports) 260 249 261 250 files := modpkg.Files() 262 251 astFiles := make([]*ast.File, len(files)) ··· 270 259 } 271 260 } 272 261 273 - forPackage := func(importPath string) *definitions.Definitions { 274 - importPath = ast.ParseImportPath(importPath).Canonical().String() 275 - for _, imported := range modpkg.Imports() { 276 - if imported.ImportPath() != importPath { 262 + forPackage := func(importPath ast.ImportPath) *definitions.Definitions { 263 + for _, importedPkg := range pkg.imports { 264 + if importedPkg.importPath != importPath { 277 265 continue 278 - } else if isUnhandledPackage(imported) { 279 - // This includes stdlib packages, which we can't jump 280 - // into yet!. TODO 281 - return nil 282 - } 283 - ip := normalizeImportPath(imported) 284 - importedPkg, found := w.findPackage(moduleRootURI(imported), ip) 285 - if !found { 286 - return nil 287 266 } 288 267 return importedPkg.definitions 289 268 }
+9 -8
internal/lsp/definitions/definitions.go
··· 346 346 // package definitions by their import path. It returns the 347 347 // Definitions for the given import path, or nil if the package cannot 348 348 // be resolved. 349 - type DefinitionsForPackageFunc = func(importPath string) *Definitions 349 + type DefinitionsForPackageFunc = func(importPath ast.ImportPath) *Definitions 350 350 351 351 // ImportersFunc is a callback function to fetch the package 352 352 // definitions for packages that directly import the current package. ··· 401 401 // parameters, see the corresponding fields in [Definitions]. 402 402 func Analyse(ip ast.ImportPath, importCanonicalisation map[string]ast.ImportPath, forPackage DefinitionsForPackageFunc, pkgImporters ImportersFunc, files ...*ast.File) *Definitions { 403 403 if forPackage == nil { 404 - forPackage = func(importPath string) *Definitions { return nil } 404 + forPackage = func(importPath ast.ImportPath) *Definitions { return nil } 405 405 } 406 406 if pkgImporters == nil { 407 407 pkgImporters = func() []*Definitions { return nil } ··· 1395 1395 // 2) In that child node, the second time we see the 1396 1396 // ImportSpec, we lookup the package imported and add a 1397 1397 // resolution to them. 1398 - str, err := strconv.Unquote(node.Path.Value) 1398 + ip := dfns.parseImportSpec(node) 1399 1399 var remotePkgDfns *Definitions 1400 - if err == nil { 1401 - remotePkgDfns = dfns.forPackage(str) 1400 + if ip != nil { 1401 + remotePkgDfns = dfns.forPackage(*ip) 1402 1402 if remotePkgDfns != nil { 1403 1403 // The pkg exists. Booting it means that its 1404 1404 // pkgDecls have contributingNodes which are the ··· 1790 1790 navigables = append(navigables, node.resolvesTo...) 1791 1791 1792 1792 if spec, ok := node.keyAlias.(*ast.ImportSpec); ok { 1793 - str, err := strconv.Unquote(spec.Path.Value) 1794 - if err != nil { 1793 + dfns := node.fdfns.dfns 1794 + ip := dfns.parseImportSpec(spec) 1795 + if ip == nil { 1795 1796 continue 1796 1797 } 1797 - dfns := node.fdfns.dfns.forPackage(str) 1798 + dfns = node.fdfns.dfns.forPackage(*ip) 1798 1799 if dfns == nil { 1799 1800 continue 1800 1801 }
+2 -2
internal/lsp/definitions/definitions_test.go
··· 3183 3183 analyse := func() testCaseAnalysis { 3184 3184 dfnsByFilename := make(map[string]*definitions.FileDefinitions) 3185 3185 dfnsByPkgName := make(map[string]*definitions.Definitions) 3186 - forPackage := func(importPath string) *definitions.Definitions { 3187 - return dfnsByPkgName[importPath] 3186 + forPackage := func(importPath ast.ImportPath) *definitions.Definitions { 3187 + return dfnsByPkgName[importPath.String()] 3188 3188 } 3189 3189 importCanonicalisation := make(map[string]ast.ImportPath) 3190 3190 analysis := testCaseAnalysis{