Skip to content

Commit

Permalink
Merge pull request #270 from cnblogs/update-post-map-when-workspace-c…
Browse files Browse the repository at this point in the history
…hanged

feat: update post-file map for changed workspace
  • Loading branch information
cnblogs-dudu authored Dec 6, 2023
2 parents 7c40f4e + 4ae5add commit 92fb7f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/ctx/cfg/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import getPlatformCfg = PlatformCfg.getPlatformCfg
import os from 'os'
import { ConfigurationTarget, Uri, workspace } from 'vscode'
import { Alert } from '@/infra/alert'
import { PostFileMapManager } from '@/service/post/post-file-map'

export namespace WorkspaceCfg {
export function getWorkspaceUri() {
Expand All @@ -23,7 +24,9 @@ export namespace WorkspaceCfg {
throw e
}

const oldWorkspaceUri = WorkspaceCfg.getWorkspaceUri()
const cfgTarget = ConfigurationTarget.Global
await getPlatformCfg()?.update('workspace', fsPath, cfgTarget)
PostFileMapManager.updateWithWorkspace(oldWorkspaceUri)
}
}
19 changes: 18 additions & 1 deletion src/service/post/post-file-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { postCategoryDataProvider } from '@/tree-view/provider/post-category-tre
import { postDataProvider } from '@/tree-view/provider/post-data-provider'
import { LocalState } from '@/ctx/local-state'
import { Uri } from 'vscode'
import { WorkspaceCfg } from '@/ctx/cfg/workspace'

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

function isUriPath(path: string) {
return path.startsWith('/')
}

export namespace PostFileMapManager {
export async function updateOrCreateMany(
arg:
Expand Down Expand Up @@ -67,7 +72,7 @@ export namespace PostFileMapManager {
if (map === undefined) return
const path = map[1]
if (path === '') return
return path.startsWith('/') ? Uri.parse(path).fsPath : path
return isUriPath(path) ? Uri.parse(path).fsPath : path
}

export function getPostId(filePath: string): number | undefined {
Expand All @@ -81,4 +86,16 @@ export namespace PostFileMapManager {
if (match == null) return
return Number(match[1])
}

export function updateWithWorkspace(oldWorkspaceUri: Uri) {
const newWorkspaceUri = WorkspaceCfg.getWorkspaceUri()
if (newWorkspaceUri.path === oldWorkspaceUri.path) return
getMaps().forEach(x => {
const filePath = x[1]
if (isUriPath(filePath) && filePath.indexOf(oldWorkspaceUri.path) >= 0)
x[1] = filePath.replace(oldWorkspaceUri.path, newWorkspaceUri.path)
else if (!isUriPath(filePath) && filePath.indexOf(oldWorkspaceUri.fsPath) >= 0)
x[1] = filePath.replace(oldWorkspaceUri.fsPath, newWorkspaceUri.fsPath)
})
}
}

0 comments on commit 92fb7f7

Please sign in to comment.