Skip to content

Commit 4ae5add

Browse files
committed
feat: update post-file map for changed workspace
1 parent 7c40f4e commit 4ae5add

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/ctx/cfg/workspace.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import getPlatformCfg = PlatformCfg.getPlatformCfg
33
import os from 'os'
44
import { ConfigurationTarget, Uri, workspace } from 'vscode'
55
import { Alert } from '@/infra/alert'
6+
import { PostFileMapManager } from '@/service/post/post-file-map'
67

78
export namespace WorkspaceCfg {
89
export function getWorkspaceUri() {
@@ -23,7 +24,9 @@ export namespace WorkspaceCfg {
2324
throw e
2425
}
2526

27+
const oldWorkspaceUri = WorkspaceCfg.getWorkspaceUri()
2628
const cfgTarget = ConfigurationTarget.Global
2729
await getPlatformCfg()?.update('workspace', fsPath, cfgTarget)
30+
PostFileMapManager.updateWithWorkspace(oldWorkspaceUri)
2831
}
2932
}

src/service/post/post-file-map.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { postCategoryDataProvider } from '@/tree-view/provider/post-category-tre
22
import { postDataProvider } from '@/tree-view/provider/post-data-provider'
33
import { LocalState } from '@/ctx/local-state'
44
import { Uri } from 'vscode'
5+
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
56

67
const validatePostFileMap = (map: PostFileMap) => map[0] >= 0 && map[1] !== ''
78
export type PostFileMap = [postId: number, filePath: string]
@@ -11,6 +12,10 @@ function getMaps(): PostFileMap[] {
1112
return <PostFileMap[]>LocalState.getState(storageKey) ?? []
1213
}
1314

15+
function isUriPath(path: string) {
16+
return path.startsWith('/')
17+
}
18+
1419
export namespace PostFileMapManager {
1520
export async function updateOrCreateMany(
1621
arg:
@@ -67,7 +72,7 @@ export namespace PostFileMapManager {
6772
if (map === undefined) return
6873
const path = map[1]
6974
if (path === '') return
70-
return path.startsWith('/') ? Uri.parse(path).fsPath : path
75+
return isUriPath(path) ? Uri.parse(path).fsPath : path
7176
}
7277

7378
export function getPostId(filePath: string): number | undefined {
@@ -81,4 +86,16 @@ export namespace PostFileMapManager {
8186
if (match == null) return
8287
return Number(match[1])
8388
}
89+
90+
export function updateWithWorkspace(oldWorkspaceUri: Uri) {
91+
const newWorkspaceUri = WorkspaceCfg.getWorkspaceUri()
92+
if (newWorkspaceUri.path === oldWorkspaceUri.path) return
93+
getMaps().forEach(x => {
94+
const filePath = x[1]
95+
if (isUriPath(filePath) && filePath.indexOf(oldWorkspaceUri.path) >= 0)
96+
x[1] = filePath.replace(oldWorkspaceUri.path, newWorkspaceUri.path)
97+
else if (!isUriPath(filePath) && filePath.indexOf(oldWorkspaceUri.fsPath) >= 0)
98+
x[1] = filePath.replace(oldWorkspaceUri.fsPath, newWorkspaceUri.fsPath)
99+
})
100+
}
84101
}

0 commit comments

Comments
 (0)