Skip to content

Commit 04b0181

Browse files
committed
Switch to integer project ids
1 parent 75191c1 commit 04b0181

File tree

6 files changed

+41
-36
lines changed

6 files changed

+41
-36
lines changed

_api/src/base/api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ export interface API<Async extends boolean> {
2121
}
2222

2323
export abstract class Project<Async extends boolean> {
24+
id: number;
2425
configFileName!: string;
2526
compilerOptions!: Record<string, unknown>;
2627
rootFiles!: readonly string[];
2728

2829
constructor(data: ProjectData) {
30+
this.id = data.id;
2931
this.loadData(data);
3032
}
3133

_api/src/bench.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ import { isIdentifier } from "./ast/nodeTests.ts";
1010
import { SymbolFlags } from "./base/api.ts";
1111
import { API } from "./sync/api.ts";
1212
{
13+
const api = new API({
14+
tsserverPath: new URL("../../built/local/tsgo", import.meta.url).pathname,
15+
cwd: dirname(new URL(import.meta.url).pathname),
16+
// logFile: "tsgo.log",
17+
});
18+
const project = api.loadProject("../../../TypeScript/src/compiler/tsconfig.json");
19+
const file = project.getSourceFile("checker.ts")!;
20+
1321
// bench("native - batched", () => {
1422
// const symbolRequests: { fileName: string; position: number; }[] = [];
1523
// file.forEachChild(function visitNode(node) {
@@ -23,14 +31,6 @@ import { API } from "./sync/api.ts";
2331
// });
2432

2533
bench("native - many calls", () => {
26-
const api = new API({
27-
tsserverPath: new URL("../../built/local/tsgo", import.meta.url).pathname,
28-
cwd: dirname(new URL(import.meta.url).pathname),
29-
// logFile: "tsgo.log",
30-
});
31-
const project = api.loadProject("../../../TypeScript/src/compiler/tsconfig.json");
32-
const file = project.getSourceFile("checker.ts")!;
33-
3434
file.forEachChild(function visitNode(node) {
3535
if (isIdentifier(node)) {
3636
const symbol = project.getSymbolAtPosition("checker.ts", node.pos);

_api/src/sync/api.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,19 @@ export class Project extends BaseProject<false> {
5252
}
5353

5454
getSourceFile(fileName: string): SourceFileNode | undefined {
55-
const data = this.client.requestBinary("getSourceFile", { project: this.configFileName, fileName });
55+
const data = this.client.requestBinary("getSourceFile", { project: this.id, fileName });
5656
return data ? new SourceFile(this.client, this, data) as unknown as SourceFileNode : undefined;
5757
}
5858

5959
getSymbolAtPosition(requests: readonly { fileName: string; position: number; }[]): (Symbol | undefined)[];
6060
getSymbolAtPosition(fileName: string, position: number): Symbol | undefined;
6161
getSymbolAtPosition(...params: [fileName: string, position: number] | [readonly { fileName: string; position: number; }[]]): Symbol | undefined | (Symbol | undefined)[] {
6262
if (params.length === 2) {
63-
const data = this.client.request("getSymbolAtPosition", { project: this.configFileName, fileName: params[0], position: params[1] });
63+
const data = this.client.request("getSymbolAtPosition", { project: this.id, fileName: params[0], position: params[1] });
6464
return data ? new Symbol(this.client, this, data) : undefined;
6565
}
6666
else {
67-
const data = this.client.request("getSymbolAtPosition", params[0].map(({ fileName, position }) => ({ project: this.configFileName, fileName, position })));
67+
const data = this.client.request("getSymbolAtPosition", params[0].map(({ fileName, position }) => ({ project: this.id, fileName, position })));
6868
return data.map((d: SymbolData | null) => d ? new Symbol(this.client, this, d) : undefined);
6969
}
7070
}
@@ -91,7 +91,7 @@ export class Symbol extends BaseSymbol<false> {
9191
}
9292

9393
getType(): Type | undefined {
94-
const data = this.client.request("getTypeOfSymbol", { project: this.project.configFileName, symbol: this.id });
94+
const data = this.client.request("getTypeOfSymbol", { project: this.project.id, symbol: this.id });
9595
return data ? new Type(this.client, data) : undefined;
9696
}
9797
}

_api/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface ParsedCommandLine {
1111
}
1212

1313
export interface ProjectData {
14+
id: number;
1415
configFileName: string;
1516
compilerOptions: Record<string, unknown>;
1617
rootFiles: string[];

internal/api/api.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type API struct {
2626
scriptInfosMu sync.RWMutex
2727
scriptInfos map[tspath.Path]*project.ScriptInfo
2828

29-
projects map[tspath.Path]*project.Project
29+
projects []*project.Project
3030
symbolsMu sync.Mutex
3131
symbols map[Handle[ast.Symbol]]*ast.Symbol
3232
}
@@ -42,7 +42,6 @@ func NewAPI(host APIHost, options APIOptions) *API {
4242
CurrentDirectory: host.GetCurrentDirectory(),
4343
}),
4444
scriptInfos: make(map[tspath.Path]*project.ScriptInfo),
45-
projects: make(map[tspath.Path]*project.Project),
4645
symbols: make(map[Handle[ast.Symbol]]*ast.Symbol),
4746
}
4847
}
@@ -105,7 +104,7 @@ func (api *API) HandleRequest(id int, method string, payload json.RawMessage) ([
105104
switch Method(method) {
106105
case MethodGetSourceFile:
107106
params := params.(*GetSourceFileParams)
108-
sourceFile, err := api.GetSourceFile(api.toPath(params.Project), params.FileName)
107+
sourceFile, err := api.GetSourceFile(params.Project, params.FileName)
109108
if err != nil {
110109
return nil, err
111110
}
@@ -116,11 +115,11 @@ func (api *API) HandleRequest(id int, method string, payload json.RawMessage) ([
116115
return encodeJSON(api.LoadProject(params.(*LoadProjectParams).ConfigFileName))
117116
case MethodGetSymbolAtPosition:
118117
return encodeJSON(handleBatchableRequest(params, func(params *GetSymbolAtPositionParams) (any, error) {
119-
return api.GetSymbolAtPosition(api.toPath(params.Project), params.FileName, int(params.Position))
118+
return api.GetSymbolAtPosition(params.Project, params.FileName, int(params.Position))
120119
}))
121120
case MethodGetTypeOfSymbol:
122121
return encodeJSON(handleBatchableRequest(params, func(params *GetTypeOfSymbolParams) (any, error) {
123-
return api.GetTypeOfSymbol(api.toPath(params.Project), params.Symbol)
122+
return api.GetTypeOfSymbol(params.Project, params.Symbol)
124123
}))
125124
default:
126125
return nil, fmt.Errorf("unhandled API method %q", method)
@@ -159,15 +158,16 @@ func (api *API) LoadProject(configFileName string) (*ProjectData, error) {
159158
return nil, err
160159
}
161160
project.GetProgram()
162-
api.projects[configFilePath] = project
163-
return NewProjectData(project), nil
161+
id := len(api.projects)
162+
api.projects = append(api.projects, project)
163+
return NewProjectData(project, id), nil
164164
}
165165

166-
func (api *API) GetSymbolAtPosition(projectPath tspath.Path, fileName string, position int) (*SymbolData, error) {
167-
project, ok := api.projects[projectPath]
168-
if !ok {
169-
return nil, fmt.Errorf("project %q not found", projectPath)
166+
func (api *API) GetSymbolAtPosition(projectId int, fileName string, position int) (*SymbolData, error) {
167+
if projectId >= len(api.projects) {
168+
return nil, fmt.Errorf("project not found")
170169
}
170+
project := api.projects[projectId]
171171
symbol, err := project.LanguageService().GetSymbolAtPosition(fileName, position)
172172
if err != nil || symbol == nil {
173173
return nil, err
@@ -179,11 +179,11 @@ func (api *API) GetSymbolAtPosition(projectPath tspath.Path, fileName string, po
179179
return data, nil
180180
}
181181

182-
func (api *API) GetTypeOfSymbol(projectPath tspath.Path, symbolHandle Handle[ast.Symbol]) (*TypeData, error) {
183-
project, ok := api.projects[projectPath]
184-
if !ok {
185-
return nil, fmt.Errorf("project %q not found", projectPath)
182+
func (api *API) GetTypeOfSymbol(projectId int, symbolHandle Handle[ast.Symbol]) (*TypeData, error) {
183+
if projectId >= len(api.projects) {
184+
return nil, fmt.Errorf("project not found")
186185
}
186+
project := api.projects[projectId]
187187
symbol, ok := api.symbols[symbolHandle]
188188
if !ok {
189189
return nil, fmt.Errorf("symbol %q not found", symbolHandle)
@@ -195,14 +195,14 @@ func (api *API) GetTypeOfSymbol(projectPath tspath.Path, symbolHandle Handle[ast
195195
return NewTypeData(t), nil
196196
}
197197

198-
func (api *API) GetSourceFile(projectPath tspath.Path, fileName string) (*ast.SourceFile, error) {
199-
project, ok := api.projects[projectPath]
200-
if !ok {
201-
return nil, fmt.Errorf("project %q not found", projectPath)
198+
func (api *API) GetSourceFile(projectId int, fileName string) (*ast.SourceFile, error) {
199+
if projectId >= len(api.projects) {
200+
return nil, fmt.Errorf("project not found")
202201
}
202+
project := api.projects[projectId]
203203
sourceFile := project.GetProgram().GetSourceFile(fileName)
204204
if sourceFile == nil {
205-
return nil, nil
205+
return nil, fmt.Errorf("source file %q not found", fileName)
206206
}
207207
return sourceFile, nil
208208
}

internal/api/proto.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,23 @@ type LoadProjectParams struct {
5656
}
5757

5858
type ProjectData struct {
59+
Id int `json:"id"`
5960
ConfigFileName string `json:"configFileName"`
6061
RootFiles []string `json:"rootFiles"`
6162
CompilerOptions *core.CompilerOptions `json:"compilerOptions"`
6263
}
6364

64-
func NewProjectData(project *project.Project) *ProjectData {
65+
func NewProjectData(project *project.Project, id int) *ProjectData {
6566
return &ProjectData{
67+
Id: id,
6668
ConfigFileName: project.Name(),
6769
RootFiles: project.GetRootFileNames(),
6870
CompilerOptions: project.GetCompilerOptions(),
6971
}
7072
}
7173

7274
type GetSymbolAtPositionParams struct {
73-
Project string `json:"project"`
75+
Project int `json:"project"`
7476
FileName string `json:"fileName"`
7577
Position uint32 `json:"position"`
7678
}
@@ -92,7 +94,7 @@ func NewSymbolData(symbol *ast.Symbol, projectVersion int) *SymbolData {
9294
}
9395

9496
type GetTypeOfSymbolParams struct {
95-
Project string `json:"project"`
97+
Project int `json:"project"`
9698
Symbol Handle[ast.Symbol] `json:"symbol"`
9799
}
98100

@@ -109,7 +111,7 @@ func NewTypeData(t *checker.Type) *TypeData {
109111
}
110112

111113
type GetSourceFileParams struct {
112-
Project string `json:"project"`
114+
Project int `json:"project"`
113115
FileName string `json:"fileName"`
114116
}
115117

0 commit comments

Comments
 (0)