SpringIndexElement serialization. Initial Structure view in VSCode

Signed-off-by: aboyko <alex.boyko@broadcom.com>
This commit is contained in:
aboyko
2025-04-15 16:58:45 -04:00
parent 260e9c9d49
commit 8ccaa5f5ef
14 changed files with 873 additions and 180 deletions

View File

@@ -0,0 +1,40 @@
import { CancellationToken, commands, Event, EventEmitter, ProviderResult, TreeDataProvider, TreeItem, TreeItemCollapsibleState } from "vscode";
import { StructureManager } from "./structure-tree-manager";
import { DocumentNode, ProjectNode, SpringNode } from "./nodes";
import * as Path from "path";
export class ExplorerTreeProvider implements TreeDataProvider<SpringNode> {
private emitter: EventEmitter<undefined | SpringNode | SpringNode[]>;
public readonly onDidChangeTreeData: Event<undefined | SpringNode | SpringNode[]>;
constructor(private manager: StructureManager) {
this.emitter = new EventEmitter<undefined | SpringNode | SpringNode[]>();
this.onDidChangeTreeData = this.emitter.event;
this.manager.onDidChange(e => this.emitter.fire(e));
}
getTreeItem(element: SpringNode): TreeItem | Thenable<TreeItem> {
return element.getTreeItem();
}
getChildren(element?: SpringNode): ProviderResult<SpringNode[]> {
if (element) {
return element.children;
}
return this.getRootElements();
}
getRootElements(): ProviderResult<SpringNode[]> {
return this.manager.rootElements;
}
// getParent?(element: SpringNode): ProviderResult<SpringNode> {
// throw new Error("Method not implemented.");
// }
// resolveTreeItem?(item: TreeItem, element: SpringNode, token: CancellationToken): ProviderResult<TreeItem> {
// throw new Error("Method not implemented.");
// }
}

View File

@@ -0,0 +1,222 @@
import { ThemeIcon, TreeItem, TreeItemCollapsibleState, Uri } from "vscode";
import { Location, Range } from "vscode-languageclient";
export class SpringNode {
constructor(readonly children: SpringNode[]) {}
getTreeItem(): TreeItem {
return new TreeItem("<node>", this.computeState(TreeItemCollapsibleState.Expanded));
}
computeState(defaultState: TreeItemCollapsibleState.Collapsed | TreeItemCollapsibleState.Expanded): TreeItemCollapsibleState {
return Array.isArray(this.children) && this.children.length ? defaultState : TreeItemCollapsibleState.None;
}
}
export class ProjectNode extends SpringNode {
constructor(readonly name: string, children: SpringNode[]) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.name;
return item;
}
}
export class DocumentNode extends SpringNode {
constructor(readonly docURI: Uri, children: SpringNode[]) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = undefined; // let VSCode derive the label from the resource URI
item.resourceUri = this.docURI;
item.iconPath = ThemeIcon.File;
return item;
}
}
export class AotProcessorNode extends SpringNode {
constructor(
children: SpringNode[],
readonly type: string,
readonly docUri: Uri
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.type;
item.resourceUri = this.docUri;
return item;
}
}
export class BeanMethodContainerNode extends SpringNode {
constructor(
children: SpringNode[],
readonly type: string,
readonly location: Location,
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.type;
return item;
}
}
export class BeanRegistrarNode extends SpringNode {
constructor(
children: SpringNode[],
readonly name: string,
readonly type: string,
readonly location: Location,
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.name;
return item;
}
}
export class ConfigPropertyNode extends SpringNode {
constructor(
children: SpringNode[],
readonly name: string,
readonly type: string,
readonly range: Range
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.name;
return item;
}
}
export class EventListenerNode extends SpringNode {
constructor(
children: SpringNode[],
readonly eventType: string,
readonly location: Location,
readonly containerBeanType: string,
readonly annotations: AnnotationMetadata[]
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.eventType;
return item;
}
}
export class EventPublisherNode extends SpringNode {
constructor(
children: SpringNode[],
readonly eventType: string,
readonly location: Location,
readonly eventTypesFromHierarchy: string[]
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.eventType;
return item;
}
}
export class QueryMethodNode extends SpringNode {
constructor(
children: SpringNode[],
readonly methodName: string,
readonly queryString: string,
readonly range: Range
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.methodName;
return item;
}
}
export class RequestMappingNode extends SpringNode {
constructor(
children: SpringNode[],
readonly path: string,
readonly httpMethods: string[],
readonly contentTypes: string[],
readonly acceptTypes: string[],
readonly symbolLabel: string,
readonly range: Range
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.path;
return item;
}
}
export class WebfluxRoutesNode extends RequestMappingNode {
constructor(
children: SpringNode[],
path: string,
httpMethods: string[],
contentTypes: string[],
acceptTypes: string[],
symbolLabel: string,
range: Range,
readonly ranges: Range[]
) {
super(children, path, httpMethods, contentTypes, acceptTypes, symbolLabel, range);
}
}
export class BeanNode extends SpringNode {
constructor(
children: SpringNode[],
readonly name: string,
readonly type: string,
readonly location: Location,
readonly injectionPoints: InjectionPoint[],
readonly supertypes: string[],
readonly annotations: AnnotationMetadata[],
readonly isConfiguration: boolean,
readonly symbolLabel: string
) {
super(children);
}
getTreeItem(): TreeItem {
const item = super.getTreeItem();
item.label = this.name;
return item;
}
}
export interface InjectionPoint {
readonly name: string;
readonly type: string;
readonly location: Location;
readonly annotations: AnnotationMetadata[];
}
export interface AnnotationMetadata {
readonly annotationType: string;
readonly isMetaAnnotation: boolean;
readonly location: Location;
readonly attributes: {[key: string]: AnnotationAttributeValue[]};
}
export interface AnnotationAttributeValue {
readonly name: string;
readonly location: Location;
}

View File

@@ -0,0 +1,124 @@
import { commands, EventEmitter, Event, Uri } from "vscode";
import { AotProcessorNode, BeanMethodContainerNode, BeanNode, BeanRegistrarNode, ConfigPropertyNode, DocumentNode, EventListenerNode, EventPublisherNode, ProjectNode, QueryMethodNode, RequestMappingNode, SpringNode, WebfluxRoutesNode } from "./nodes";
const SPRING_STRUCTURE_CMD = "sts/spring-boot/structure";
export class StructureManager {
private _rootElements: Thenable<SpringNode[]>
private _onDidChange: EventEmitter<SpringNode | undefined> = new EventEmitter<SpringNode | undefined>();
get rootElements(): Thenable<SpringNode[]> {
return this._rootElements;
}
refresh(): void {
this._rootElements = commands.executeCommand(SPRING_STRUCTURE_CMD).then(json => {
const nodes = this.parseArray(json);
this._onDidChange.fire(undefined);
return nodes;
});
}
private parseNode(json: any): SpringNode | undefined {
if (typeof (json._internal_node_type) === 'string') {
switch (json._internal_node_type) {
case "ProjectElement":
return new ProjectNode(json.projectName as string, this.parseArray(json.children));
case "DocumentElement":
return new DocumentNode(Uri.parse(json.docURI as string), this.parseArray(json.children));
case "Bean":
return new BeanNode(
this.parseArray(json.children),
json.name,
json.type,
json.location,
json.injectionPoints,
json.supertypes,
json.annotations,
json.isConfiguration,
json.symbolLabel
);
case "AotProcessorElement":
return new AotProcessorNode(
this.parseArray(json.children),
json.name,
Uri.parse(json.docUri)
);
case "BeanMethodContainerElement":
return new BeanMethodContainerNode(
this.parseArray(json.children),
json.type,
json.location
);
case "BeanRegistrarElement":
return new BeanRegistrarNode(
this.parseArray(json.children),
json.name,
json.type,
json.location
);
case "ConfigPropertyIndexElement":
return new ConfigPropertyNode(
this.parseArray(json.children),
json.name,
json.type,
json.range
);
case "EventListenerIndexElement":
return new EventListenerNode(
this.parseArray(json.children),
json.eventType,
json.location,
json.containerBeanType,
json.annotations
);
case "EventPublisherIndexElement":
return new EventPublisherNode(
this.parseArray(json.children),
json.eventType,
json.location,
json.eventTypesFromHierarchy,
);
case "QueryMethodIndexElement":
return new QueryMethodNode(
this.parseArray(json.children),
json.methodName,
json.queryString,
json.range
);
case "RequestMappingIndexElement":
return new RequestMappingNode(
this.parseArray(json.children),
json.path,
json.httpMethods,
json.contentTypes,
json.acceptTypes,
json.symbolLabel,
json.range
);
case "WebfluxRouteElementRangesIndexElement":
return new WebfluxRoutesNode(
this.parseArray(json.children),
json.path,
json.httpMethods,
json.contentTypes,
json.acceptTypes,
json.symbolLabel,
json.range,
json.ranges
);
}
}
}
private parseArray(json: any): SpringNode[] {
return Array.isArray(json) ? (json as []).map(j => this.parseNode(j)).filter(e => !!e) : [];
}
public get onDidChange(): Event<SpringNode | undefined> {
return this._onDidChange.event;
}
}