1- import {
2- workspace ,
3- ExtensionContext ,
4- window ,
5- commands ,
6- Range ,
7- TreeItem ,
8- TreeItemLabel ,
9- TreeItemCollapsibleState ,
10- TreeDataProvider ,
11- TextDocumentShowOptions ,
12- Uri ,
13- ThemeIcon ,
14- EventEmitter ,
15- Position
16- } from 'vscode'
1+ import { workspace , ExtensionContext , window , commands } from 'vscode'
172
183import {
194 LanguageClient ,
@@ -23,7 +8,6 @@ import {
238} from 'vscode-languageclient/node'
249
2510import { activate as activateWebview } from './view'
26- import { SgSearch } from './types'
2711
2812let client : LanguageClient
2913const diagnosticCollectionName = 'ast-grep-diagnostics'
@@ -47,108 +31,6 @@ function getExecutable(isDebug: boolean): Executable {
4731 }
4832}
4933
50- interface FileItem {
51- file : string
52- }
53-
54- interface SearchItem {
55- file : string
56- source : string
57- range : Range
58- }
59- class AstGrepScanTreeItem extends TreeItem {
60- constructor ( public item : FileItem | SearchItem ) {
61- let label
62- let collapsibleState = TreeItemCollapsibleState . None
63- if ( 'source' in item ) {
64- const { start, end } = item . range
65- label = {
66- label : item . source ,
67- highlights : [ [ start . character , end . character ] ]
68- } as TreeItemLabel
69- } else {
70- label = item . file
71- collapsibleState = TreeItemCollapsibleState . Expanded
72- }
73- super ( label , collapsibleState )
74- if ( 'source' in item ) {
75- this . command = {
76- title : 'ast-grep' ,
77- command : 'vscode.open' ,
78- arguments : [
79- this . uri ,
80- < TextDocumentShowOptions > {
81- selection : item . range
82- }
83- ]
84- }
85- }
86- }
87-
88- get uri ( ) {
89- // Get the current workspace folder
90- const workspaceFolder = workspace . workspaceFolders ! [ 0 ]
91- // Join the workspace folder path with the relative path
92- const filePath = Uri . joinPath ( workspaceFolder . uri , this . item . file )
93- return filePath
94- }
95-
96- static isSearchItem ( item : FileItem | SearchItem ) : item is SearchItem {
97- return 'source' in item
98- }
99- }
100-
101- type Dictionary < T > = { [ key : string ] : T }
102- export class AstGrepSearchResultProvider
103- implements TreeDataProvider < AstGrepScanTreeItem >
104- {
105- private scanResultDict : Dictionary < SgSearch [ ] > = { }
106- private emitter = new EventEmitter < undefined > ( )
107- onDidChangeTreeData = this . emitter . event
108-
109- getTreeItem ( element : AstGrepScanTreeItem ) : TreeItem {
110- // only add iconPath if the element is not a file item
111- if ( ! ( 'source' in element . item ) ) {
112- element . contextValue = 'file-item'
113- element . description = true
114- element . iconPath = ThemeIcon . File
115- element . resourceUri = element . uri
116- }
117- return element
118- }
119-
120- getChildren ( element ?: AstGrepScanTreeItem ) : Thenable < AstGrepScanTreeItem [ ] > {
121- if ( ! element ) {
122- let list = Object . keys ( this . scanResultDict ) . map ( file => {
123- return new AstGrepScanTreeItem ( { file } )
124- } )
125- return Promise . resolve ( list )
126- }
127- if ( AstGrepScanTreeItem . isSearchItem ( element . item ) ) {
128- return Promise . resolve ( [ ] )
129- }
130- let file = element . item . file
131- let list = this . scanResultDict [ file ] . map ( item => {
132- const { start, end } = item . range
133- return new AstGrepScanTreeItem ( {
134- file : item . file ,
135- source : item . lines ,
136- range : new Range (
137- new Position ( start . line , start . column ) ,
138- new Position ( end . line , end . column )
139- )
140- } )
141- } )
142- return Promise . resolve ( list )
143- }
144-
145- updateResult ( res : SgSearch [ ] ) {
146- let grouped = groupBy ( res , 'file' )
147- this . scanResultDict = grouped
148- this . emitter . fire ( undefined )
149- }
150- }
151-
15234function activateLsp ( context : ExtensionContext ) {
15335 context . subscriptions . push (
15436 commands . registerCommand ( 'ast-grep.restartLanguageServer' , async ( ) => {
@@ -196,10 +78,8 @@ function activateLsp(context: ExtensionContext) {
19678}
19779
19880export function activate ( context : ExtensionContext ) {
199- let provider = new AstGrepSearchResultProvider ( )
20081 activateLsp ( context )
201- // TODO: fix shit code
202- activateWebview ( context , provider )
82+ activateWebview ( context )
20383}
20484
20585async function restart ( ) : Promise < void > {
@@ -215,17 +95,3 @@ export function deactivate(): Promise<void> | undefined {
21595 }
21696 return client . stop ( )
21797}
218-
219- function groupBy < T extends object > ( obj : T [ ] , key : keyof T ) {
220- return obj . reduce (
221- ( acc , cur ) => {
222- let k = cur [ key ] as string
223- if ( ! acc [ k ] ) {
224- acc [ k ] = [ ]
225- }
226- acc [ k ] . push ( cur )
227- return acc
228- } ,
229- { } as Record < string , T [ ] >
230- )
231- }
0 commit comments