Skip to content

Commit 0f6ae4b

Browse files
committed
feat: add 'Find in API' and 'Find in Mock' commands with integration into respective tree views
1 parent 420afd7 commit 0f6ae4b

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ A VS Code extension for managing and generating code from YAPI APIs.
4343
| `crabu.copyApiPath` | Copy API Path |
4444
| `crabu.aiGenerateMock` | AI Generate Mock |
4545
| `crabu.updateAiQueueStatus` | Crabu: Update AI Queue Status |
46+
| `crabu.findInApi` | Crabu: Find in API |
47+
| `crabu.findInMock` | Crabu: Find in Mock |
4648

4749
<!-- commands -->
4850

package.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@
146146
"command": "crabu.updateAiQueueStatus",
147147
"title": "Update AI Queue Status",
148148
"category": "Crabu"
149+
},
150+
{
151+
"command": "crabu.findInApi",
152+
"title": "Find in API",
153+
"icon": "$(search)",
154+
"category": "Crabu",
155+
"enablement": "view == apiTreeView"
156+
},
157+
{
158+
"command": "crabu.findInMock",
159+
"title": "Find in Mock",
160+
"icon": "$(search)",
161+
"category": "Crabu",
162+
"enablement": "view == mockTreeView"
149163
}
150164
],
151165
"configuration": {
@@ -231,10 +245,15 @@
231245
"menus": {
232246
"view/title": [
233247
{
234-
"command": "crabu.searchApi",
248+
"command": "crabu.findInApi",
235249
"when": "view == apiTreeView",
236250
"group": "navigation"
237251
},
252+
{
253+
"command": "crabu.findInMock",
254+
"when": "view == mockTreeView",
255+
"group": "navigation"
256+
},
238257
{
239258
"command": "crabu.searchApiGroup",
240259
"when": "view == apiTreeView",

src/views/api.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,21 @@ async function getYapiMenuData(projectId?: number, token?: string) {
3636
export const useApiTreeView = createSingletonComposable(async () => {
3737
const roots = ref<TreeViewNode[]>([])
3838

39+
const treeView = useTreeView(
40+
'apiTreeView',
41+
roots,
42+
{
43+
showCollapseAll: true,
44+
},
45+
)
46+
3947
async function getRootNode(projects: Project[]) {
4048
return await Promise.all(projects.map(async project => ({
4149
children: await getChildNodes(project),
4250
treeItem: {
4351
label: project.name,
44-
collapsibleState: TreeItemCollapsibleState.Collapsed,
52+
contextValue: 'apiProject',
53+
collapsibleState: TreeItemCollapsibleState.Expanded,
4554
},
4655
})))
4756
}
@@ -258,11 +267,8 @@ export const useApiTreeView = createSingletonComposable(async () => {
258267
executeCommand(commands.addApiGroupToMock, selection?.node)
259268
})
260269

261-
return useTreeView(
262-
'apiTreeView',
263-
roots,
264-
{
265-
showCollapseAll: true,
266-
},
267-
)
270+
useCommand(commands.findInApi, async () => {
271+
await treeView.reveal(roots.value?.[0], { focus: true })
272+
executeCommand('list.find')
273+
})
268274
})

src/views/mock.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { TreeViewNode } from 'reactive-vscode'
22
import type { ApiDetail, MockApiData } from '../types'
33
import { parse, stringify } from 'comment-json'
4-
import { createSingletonComposable, executeCommand, ref, useCommand, useTreeView } from 'reactive-vscode'
4+
import { createSingletonComposable, executeCommand, ref, useCommand, useTreeView, watchEffect } from 'reactive-vscode'
55
import { TreeItemCollapsibleState, Uri, window, workspace } from 'vscode'
66
import { config } from '../config'
77
import { crabuDiffNewScheme, crabuDiffOldScheme } from '../constants'
@@ -48,6 +48,13 @@ function handleData(data: MockApiData[]) {
4848

4949
export const useMockTreeView = createSingletonComposable(async () => {
5050
const roots = ref<TreeViewNode[]>([])
51+
const treeView = useTreeView(
52+
'mockTreeView',
53+
roots,
54+
{
55+
showCollapseAll: true,
56+
},
57+
)
5158

5259
async function getRootNode() {
5360
try {
@@ -65,6 +72,7 @@ export const useMockTreeView = createSingletonComposable(async () => {
6572
}
6673

6774
async function refreshMockTreeView() {
75+
logger.info('refreshMockTreeView')
6876
window.withProgress({
6977
location: { viewId: 'mockTreeView' },
7078
}, async (progress) => {
@@ -73,7 +81,7 @@ export const useMockTreeView = createSingletonComposable(async () => {
7381
})
7482
}
7583

76-
await refreshMockTreeView()
84+
watchEffect(refreshMockTreeView)
7785

7886
useCommand(commands.refreshMockTreeView, refreshMockTreeView)
7987
useCommand(commands.showCrabuWebviewWithMock, (event) => {
@@ -199,11 +207,8 @@ export const useMockTreeView = createSingletonComposable(async () => {
199207
executeCommand('vscode.diff', oldUri, newUri, `检查变更:${mockItem.label}`)
200208
})
201209

202-
return useTreeView(
203-
'mockTreeView',
204-
roots,
205-
{
206-
showCollapseAll: true,
207-
},
208-
)
210+
useCommand(commands.findInMock, async () => {
211+
await treeView.reveal(roots.value?.[0], { focus: true })
212+
executeCommand('list.find')
213+
})
209214
})

0 commit comments

Comments
 (0)