Skip to content

Commit 8d868e8

Browse files
committed
feat: python parser skeleton with custom pylsp
Custom pylsp is based on [python-lsp-server](https://github.com/python-lsp/python-lsp-server), and plus the following pull requests: 1. semanticTokens/full: python-lsp/python-lsp-server#645 2. typeDefinition: python-lsp/python-lsp-server#533 Maybe also 3. implementation: python-lsp/python-lsp-server#644
1 parent fe8fb67 commit 8d868e8

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ ABCoder currently supports the following languages:
5252
| Go |||
5353
| Rust || Coming Soon |
5454
| C | Coming Soon ||
55+
| Python | Coming Soon ||
5556

5657

5758

lang/collect/collect.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"unicode"
2424

2525
"github.com/cloudwego/abcoder/lang/cxx"
26+
"github.com/cloudwego/abcoder/lang/python"
2627
"github.com/cloudwego/abcoder/lang/log"
2728
. "github.com/cloudwego/abcoder/lang/lsp"
2829
"github.com/cloudwego/abcoder/lang/rust"
@@ -82,6 +83,8 @@ func switchSpec(l uniast.Language) LanguageSpec {
8283
return &rust.RustSpec{}
8384
case uniast.Cxx:
8485
return &cxx.CxxSpec{}
86+
case uniast.Python:
87+
return &python.PythonSpec{}
8588
default:
8689
panic(fmt.Sprintf("unsupported language %s", l))
8790
}

lang/parse.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/cloudwego/abcoder/lang/collect"
2929
"github.com/cloudwego/abcoder/lang/cxx"
30+
"github.com/cloudwego/abcoder/lang/python"
3031
"github.com/cloudwego/abcoder/lang/golang/parser"
3132
"github.com/cloudwego/abcoder/lang/log"
3233
"github.com/cloudwego/abcoder/lang/lsp"
@@ -97,6 +98,9 @@ func checkRepoPath(repoPath string, language uniast.Language) (openfile string,
9798
openfile, wait = rust.CheckRepo(repoPath)
9899
case uniast.Cxx:
99100
openfile, wait = cxx.CheckRepo(repoPath)
101+
case uniast.Python:
102+
// NOTICE: open the Cargo.toml file is required for Rust projects
103+
openfile, wait = python.CheckRepo(repoPath)
100104
default:
101105
openfile = ""
102106
wait = 0
@@ -112,6 +116,8 @@ func checkLSP(language uniast.Language, lspPath string) (l uniast.Language, s st
112116
l, s = rust.GetDefaultLSP()
113117
case uniast.Cxx:
114118
l, s = cxx.GetDefaultLSP()
119+
case uniast.Python:
120+
l, s = python.GetDefaultLSP()
115121
case uniast.Golang:
116122
l = uniast.Golang
117123
s = ""

lang/python/lib.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2025 CloudWeGo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package python
16+
17+
import (
18+
"time"
19+
20+
"github.com/cloudwego/abcoder/lang/uniast"
21+
"github.com/cloudwego/abcoder/lang/utils"
22+
)
23+
24+
const MaxWaitDuration = 5 * time.Minute
25+
26+
func GetDefaultLSP() (lang uniast.Language, name string) {
27+
// needs to use the pylsp from https://github.com/python-lsp/python-lsp-server/pull/533
28+
return uniast.Python, "pylsp"
29+
}
30+
31+
func CheckRepo(repo string) (string, time.Duration) {
32+
openfile := ""
33+
// TODO: check if the project compiles.
34+
35+
// NOTICE: wait for Rust projects based on code files
36+
_, size := utils.CountFiles(repo, ".py", "SKIPDIR")
37+
wait := 2*time.Second + time.Second*time.Duration(size/1024)
38+
if wait > MaxWaitDuration {
39+
wait = MaxWaitDuration
40+
}
41+
return openfile, wait
42+
}

lang/python/spec.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2025 CloudWeGo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package python
16+
17+
import (
18+
lsp "github.com/cloudwego/abcoder/lang/lsp"
19+
)
20+
21+
type PythonSpec struct {
22+
repo string
23+
}
24+
25+
func NewPythonSpec() *PythonSpec {
26+
return &PythonSpec{}
27+
}
28+
29+
func (c *PythonSpec) WorkSpace(root string) (map[string]string, error) {
30+
panic("TODO")
31+
}
32+
33+
func (c *PythonSpec) NameSpace(path string) (string, string, error) {
34+
panic("TODO")
35+
}
36+
37+
func (c *PythonSpec) ShouldSkip(path string) bool {
38+
panic("TODO")
39+
}
40+
41+
func (c *PythonSpec) DeclareTokenOfSymbol(sym lsp.DocumentSymbol) int {
42+
panic("TODO")
43+
}
44+
45+
func (c *PythonSpec) IsEntityToken(tok lsp.Token) bool {
46+
panic("TODO")
47+
}
48+
49+
func (c *PythonSpec) IsStdToken(tok lsp.Token) bool {
50+
panic("TODO")
51+
}
52+
53+
func (c *PythonSpec) TokenKind(tok lsp.Token) lsp.SymbolKind {
54+
panic("TODO")
55+
}
56+
57+
func (c *PythonSpec) IsMainFunction(sym lsp.DocumentSymbol) bool {
58+
panic("TODO")
59+
}
60+
61+
func (c *PythonSpec) IsEntitySymbol(sym lsp.DocumentSymbol) bool {
62+
panic("TODO")
63+
}
64+
65+
func (c *PythonSpec) IsPublicSymbol(sym lsp.DocumentSymbol) bool {
66+
panic("TODO")
67+
}
68+
69+
func (c *PythonSpec) HasImplSymbol() bool {
70+
panic("TODO")
71+
}
72+
73+
func (c *PythonSpec) ImplSymbol(sym lsp.DocumentSymbol) (int, int, int) {
74+
panic("TODO")
75+
}
76+
77+
func (c *PythonSpec) FunctionSymbol(sym lsp.DocumentSymbol) (int, []int, []int, []int) {
78+
panic("TODO")
79+
}
80+
81+
func (c *PythonSpec) GetUnloadedSymbol(from lsp.Token, define lsp.Location) (string, error) {
82+
panic("TODO")
83+
}

lang/uniast/ast.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const (
2929
Golang Language = "go"
3030
Rust Language = "rust"
3131
Cxx Language = "cxx"
32+
Python Language = "python"
3233
Unknown Language = ""
3334
)
3435

@@ -40,6 +41,8 @@ func (l Language) String() string {
4041
return "go"
4142
case Cxx:
4243
return "cxx"
44+
case Python:
45+
return "python"
4346
default:
4447
return string(l)
4548
}
@@ -58,6 +61,8 @@ func NewLanguage(lang string) (l Language) {
5861
return Rust
5962
case "cxx":
6063
return Cxx
64+
case "python":
65+
return Python
6166
default:
6267
return Unknown
6368
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Action:
5151
Language:
5252
rust for rust codes
5353
cxx for c codes (cpp support is on the way)
54+
python for python codes
5455
go for golang codes
5556
URI:
5657
for action parse: the directory path of the repo

0 commit comments

Comments
 (0)