Skip to content

Commit 43e3cff

Browse files
committed
fix(python): allow ignore bultin, separate site-packages from builtin
1 parent 4daab2f commit 43e3cff

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

lang/collect/collect.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,30 @@ func NewCollector(repo string, cli *LSPClient) *Collector {
113113
return ret
114114
}
115115

116+
func (c *Collector) configureLSP(ctx context.Context) {
117+
// XXX: should be put in language specification
118+
if c.Language == uniast.Python {
119+
if !c.NeedStdSymbol {
120+
if c.Language == uniast.Python {
121+
conf := map[string]interface{}{
122+
"settings": map[string]interface{}{
123+
"pylsp": map[string]interface{}{
124+
"plugins": map[string]interface{}{
125+
"jedi_definition": map[string]interface{}{
126+
"follow_builtin_definitions": false,
127+
},
128+
},
129+
},
130+
},
131+
}
132+
c.cli.Notify(ctx, "workspace/didChangeConfiguration", conf)
133+
}
134+
}
135+
}
136+
}
137+
116138
func (c *Collector) Collect(ctx context.Context) error {
139+
c.configureLSP(ctx)
117140
excludes := make([]string, len(c.Excludes))
118141
for i, e := range c.Excludes {
119142
if !filepath.IsAbs(e) {

lang/python/spec.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ func (c *PythonSpec) NameSpace(path string) (string, string, error) {
9191
// XXX: hardcoded python path
9292
condaPrefix := "/home/zhenyang/anaconda3/envs/abcoder/lib/python3.11"
9393
if strings.HasPrefix(path, condaPrefix) {
94+
if strings.HasPrefix(path, condaPrefix+"/site-packages") {
95+
// external module
96+
relPath, err := filepath.Rel(condaPrefix+"/site-packages", path)
97+
if err != nil {
98+
return "", "", err
99+
}
100+
relPath = strings.TrimSuffix(relPath, ".py")
101+
pkgPath := strings.ReplaceAll(relPath, string(os.PathSeparator), ".")
102+
modPath := strings.Split(pkgPath, ".")
103+
if len(modPath) >= 1 {
104+
modName := modPath[0]
105+
return modName, pkgPath, nil
106+
}
107+
panic(fmt.Sprintf("Malformed Namespace %s, pkgPath %s", path, pkgPath))
108+
}
109+
// builtin module
94110
modName := "builtins"
95111
relPath, err := filepath.Rel(condaPrefix, path)
96112
if err != nil {
@@ -101,7 +117,7 @@ func (c *PythonSpec) NameSpace(path string) (string, string, error) {
101117
return modName, pkgPath, nil
102118
}
103119

104-
panic(fmt.Sprintf("Namespace %s", path))
120+
panic(fmt.Sprintf("Unhandled Namespace %s", path))
105121
}
106122

107123
func (c *PythonSpec) ShouldSkip(path string) bool {

0 commit comments

Comments
 (0)