generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 30
feat: support ts #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Duslia
wants to merge
1
commit into
cloudwego:main
Choose a base branch
from
Duslia:feat/support_ts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: support ts #83
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2025 CloudWeGo Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package typescript | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/cloudwego/abcoder/lang/uniast" | ||
"github.com/cloudwego/abcoder/lang/utils" | ||
) | ||
|
||
const MaxWaitDuration = 5 * time.Second | ||
|
||
func GetDefaultLSP() (lang uniast.Language, name string) { | ||
return uniast.TypeScript, "typescript-language-server" | ||
} | ||
|
||
func CheckRepo(repo string) (string, time.Duration) { | ||
openfile := "" | ||
|
||
// Give the LSP sometime to initialize | ||
_, size := utils.CountFiles(repo, ".ts", "node_modules/") | ||
wait := 2*time.Second + time.Second*time.Duration(size/1024) | ||
if wait > MaxWaitDuration { | ||
wait = MaxWaitDuration | ||
} | ||
return openfile, wait | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
// Copyright 2025 CloudWeGo Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package typescript | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
lsp "github.com/cloudwego/abcoder/lang/lsp" | ||
"github.com/cloudwego/abcoder/lang/uniast" | ||
) | ||
|
||
var _ lsp.LanguageSpec = (*TypeScriptSpec)(nil) | ||
|
||
type TypeScriptSpec struct { | ||
repo string | ||
} | ||
|
||
func NewTypeScriptSpec() *TypeScriptSpec { | ||
return &TypeScriptSpec{} | ||
} | ||
|
||
func (c *TypeScriptSpec) FileImports(content []byte) ([]uniast.Import, error) { | ||
// TODO: Parse TypeScript import statements | ||
return []uniast.Import{}, nil | ||
} | ||
|
||
func (c *TypeScriptSpec) IsExternalEntityToken(tok lsp.Token) bool { | ||
if !c.IsEntityToken(tok) { | ||
return false | ||
} | ||
for _, m := range tok.Modifiers { | ||
if m == "defaultLibrary" { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (c *TypeScriptSpec) TokenKind(tok lsp.Token) lsp.SymbolKind { | ||
switch tok.Type { | ||
case "class": | ||
return lsp.SKClass | ||
case "interface": | ||
return lsp.SKInterface | ||
case "function": | ||
return lsp.SKFunction | ||
case "method": | ||
return lsp.SKMethod | ||
case "property": | ||
return lsp.SKProperty | ||
case "variable": | ||
return lsp.SKVariable | ||
case "const": | ||
return lsp.SKConstant | ||
case "enum": | ||
return lsp.SKEnum | ||
case "enumMember": | ||
return lsp.SKEnumMember | ||
case "type": | ||
return lsp.SKTypeParameter | ||
case "namespace": | ||
return lsp.SKNamespace | ||
case "module": | ||
return lsp.SKModule | ||
default: | ||
return lsp.SKUnknown | ||
} | ||
} | ||
|
||
func (c *TypeScriptSpec) IsStdToken(tok lsp.Token) bool { | ||
for _, m := range tok.Modifiers { | ||
if m == "defaultLibrary" { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (c *TypeScriptSpec) IsDocToken(tok lsp.Token) bool { | ||
for _, m := range tok.Modifiers { | ||
if m == "documentation" { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (c *TypeScriptSpec) DeclareTokenOfSymbol(sym lsp.DocumentSymbol) int { | ||
for i, t := range sym.Tokens { | ||
if c.IsDocToken(t) { | ||
continue | ||
} | ||
for _, m := range t.Modifiers { | ||
if m == "declaration" { | ||
return i | ||
} | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func (c *TypeScriptSpec) IsPublicSymbol(sym lsp.DocumentSymbol) bool { | ||
// In TypeScript, symbols are public by default unless marked private/protected | ||
id := c.DeclareTokenOfSymbol(sym) | ||
if id == -1 { | ||
return true | ||
} | ||
for _, m := range sym.Tokens[id].Modifiers { | ||
if m == "private" || m == "protected" { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func (c *TypeScriptSpec) IsMainFunction(sym lsp.DocumentSymbol) bool { | ||
// TypeScript doesn't have a main function concept | ||
return false | ||
} | ||
|
||
func (c *TypeScriptSpec) IsEntitySymbol(sym lsp.DocumentSymbol) bool { | ||
typ := sym.Kind | ||
return typ == lsp.SKClass || typ == lsp.SKMethod || typ == lsp.SKFunction || | ||
typ == lsp.SKVariable || typ == lsp.SKInterface || typ == lsp.SKConstant || | ||
typ == lsp.SKEnum || typ == lsp.SKTypeParameter || typ == lsp.SKNamespace || | ||
typ == lsp.SKModule | ||
} | ||
|
||
func (c *TypeScriptSpec) IsEntityToken(tok lsp.Token) bool { | ||
typ := tok.Type | ||
return typ == "class" || typ == "interface" || typ == "function" || | ||
typ == "method" || typ == "property" || typ == "variable" || | ||
typ == "const" || typ == "enum" || typ == "enumMember" || | ||
typ == "type" || typ == "namespace" || typ == "module" | ||
} | ||
|
||
func (c *TypeScriptSpec) HasImplSymbol() bool { | ||
// TypeScript uses class/interface implementation, not impl blocks like Rust | ||
return false | ||
} | ||
|
||
func (c *TypeScriptSpec) ImplSymbol(sym lsp.DocumentSymbol) (int, int, int) { | ||
// TypeScript doesn't have impl blocks | ||
return -1, -1, -1 | ||
} | ||
|
||
func (c *TypeScriptSpec) FunctionSymbol(sym lsp.DocumentSymbol) (int, []int, []int, []int) { | ||
// TODO: Implement TypeScript function parsing | ||
return -1, nil, nil, nil | ||
} | ||
|
||
func (c *TypeScriptSpec) ShouldSkip(path string) bool { | ||
if strings.Contains(path, "/node_modules/") { | ||
return true | ||
} | ||
if !strings.HasSuffix(path, ".ts") && !strings.HasSuffix(path, ".tsx") { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (c *TypeScriptSpec) NameSpace(path string) (string, string, error) { | ||
if !strings.HasPrefix(path, c.repo) { | ||
// External module | ||
return "", "", fmt.Errorf("external module: %s", path) | ||
} | ||
|
||
// Calculate relative path from repo root | ||
rel, err := filepath.Rel(c.repo, path) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
// Remove file extension | ||
rel = strings.TrimSuffix(rel, ".ts") | ||
rel = strings.TrimSuffix(rel, ".tsx") | ||
|
||
// Remove index suffix if present | ||
if strings.HasSuffix(rel, "/index") { | ||
rel = strings.TrimSuffix(rel, "/index") | ||
} | ||
|
||
// Convert path to module name | ||
module := strings.ReplaceAll(rel, string(filepath.Separator), ".") | ||
|
||
return module, module, nil | ||
} | ||
|
||
func (c *TypeScriptSpec) WorkSpace(root string) (map[string]string, error) { | ||
c.repo = root | ||
// For TypeScript, we don't need to collect modules like Rust | ||
// The module system is based on file paths | ||
return map[string]string{}, nil | ||
} | ||
|
||
func (c *TypeScriptSpec) GetUnloadedSymbol(from lsp.Token, loc lsp.Location) (string, error) { | ||
// TODO: Implement TypeScript unloaded symbol extraction | ||
return "", nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这东西现在有用么?我看前面不也是走到ts-parser?