-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(bookmark): 修复
Refactor
指令中的Move to new file
书签异常问题
- Loading branch information
Showing
14 changed files
with
1,207 additions
and
649 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,87 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { | ||
Disposable, | ||
workspace, | ||
WorkspaceFolder, | ||
Event, | ||
EventEmitter, | ||
FileSystemWatcher, | ||
} from 'vscode'; | ||
import {WorkspaceFolder, Event, EventEmitter, extensions} from 'vscode'; | ||
import {ServiceManager} from './ServiceManager'; | ||
import {BaseService} from './BaseService'; | ||
import {API, GitExtension} from '../types'; | ||
|
||
/** | ||
* Git 相关的服务类 | ||
*/ | ||
export default class GitService extends BaseService { | ||
repos: WorkspaceFolder[] = []; | ||
private _fsWatcher: FileSystemWatcher | undefined; | ||
private _onDidReposChangeEvent: EventEmitter<WorkspaceFolder[]> = | ||
new EventEmitter<WorkspaceFolder[]>(); | ||
|
||
private _gitExtension: GitExtension | undefined; | ||
|
||
private _git: API | undefined; | ||
|
||
onDidReposChangeEvent: Event<WorkspaceFolder[]> = | ||
this._onDidReposChangeEvent.event; | ||
|
||
private _branch: string = ''; | ||
|
||
get branch() { | ||
return this._branch; | ||
} | ||
|
||
get git() { | ||
if (!this._git) { | ||
this._git = this._gitExtension?.getAPI(1); | ||
} | ||
return this._git; | ||
} | ||
|
||
get gitExtension() { | ||
if (!this._gitExtension) { | ||
try { | ||
this._gitExtension = | ||
extensions.getExtension<GitExtension>('vscode.git')!.exports; | ||
} catch (e) { | ||
this.logger.error(e); | ||
} | ||
} | ||
return this._gitExtension; | ||
} | ||
|
||
constructor(sm: ServiceManager) { | ||
super(GitService.name, sm); | ||
this._init(); | ||
workspace.onDidChangeWorkspaceFolders(ev => { | ||
const {added, removed} = ev; | ||
let isChanged = false; | ||
for (let wsFolder of added) { | ||
if (this._checkFolderIsRepo(wsFolder.uri.fsPath)) { | ||
this.repos.push(wsFolder); | ||
if (!isChanged) { | ||
isChanged = true; | ||
} | ||
} | ||
} | ||
for (let wsFolder of removed) { | ||
if (this._checkFolderIsRepo(wsFolder.uri.fsPath)) { | ||
this.repos = this.repos.filter( | ||
it => it.uri.fsPath === wsFolder.uri.fsPath, | ||
); | ||
if (!isChanged) { | ||
isChanged = true; | ||
} | ||
} | ||
} | ||
if (isChanged) { | ||
this._onDidReposChangeEvent.fire(this.repos); | ||
} | ||
}); | ||
} | ||
|
||
private _init() { | ||
const workspaceFolders = workspace.workspaceFolders || []; | ||
if (!workspaceFolders.length) { | ||
return; | ||
} | ||
for (let wsFolder of workspaceFolders) { | ||
const wsPath = wsFolder.uri.fsPath; | ||
if ( | ||
this._checkFolderIsRepo(wsFolder.uri.fsPath) && | ||
!this.repos.find(it => it.uri.fsPath === wsPath) | ||
) { | ||
this.repos.push(wsFolder); | ||
const _gitExt = extensions.all.find(it => it.id === 'vscode.git'); | ||
if (_gitExt) { | ||
if (!_gitExt.isActive) { | ||
_gitExt.activate().then(e => { | ||
this._gitExtension = e as GitExtension; | ||
this._git = this._gitExtension.getAPI(1); | ||
}); | ||
} else { | ||
this._gitExtension = | ||
extensions.getExtension<GitExtension>('vscode.git')!.exports; | ||
this._git = this._gitExtension.getAPI(1); | ||
} | ||
} | ||
} | ||
|
||
private _checkFolderIsRepo(p: string) { | ||
return fs.existsSync(path.resolve(p, '.git')); | ||
} | ||
this.addToDisposers( | ||
extensions.onDidChange(e => { | ||
try { | ||
const _gitExtension = extensions.all.find( | ||
it => it.id === 'vscode.git', | ||
); | ||
if (!_gitExtension || _gitExtension.isActive || this._gitExtension) { | ||
return; | ||
} | ||
|
||
dispose() { | ||
this._fsWatcher?.dispose(); | ||
this._gitExtension = | ||
extensions.getExtension<GitExtension>('vscode.git')!.exports; | ||
this._git = this._gitExtension.getAPI(1); | ||
} catch (error) { | ||
this.logger.error(error); | ||
} | ||
}), | ||
); | ||
} | ||
|
||
dispose() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {Instance, types} from 'mobx-state-tree'; | ||
/** | ||
* @zh 书签源代码控制提交信息 | ||
*/ | ||
export const BookmarkSCMCommitInfo = types | ||
.model('BookmarkSCMCommitInfo', { | ||
/** | ||
* @zh 创建书签时的提交的HASH | ||
*/ | ||
commitHash: types.string, | ||
|
||
/** | ||
* @zh 创建书签时的分支 | ||
*/ | ||
branch: types.string, | ||
|
||
/** | ||
* @zh 书签快照, 跟随`commitHash` | ||
*/ | ||
snapshot: types.string, | ||
}) | ||
.views(self => { | ||
return {}; | ||
}) | ||
.actions(self => { | ||
return {}; | ||
}); | ||
|
||
export type BookmarkSCMCommitInfoType = Instance<typeof BookmarkSCMCommitInfo>; |
Oops, something went wrong.