Skip to content

Commit 4657bc1

Browse files
committed
Fixed some commands to work with command palette
1 parent 953acf8 commit 4657bc1

File tree

4 files changed

+104
-71
lines changed

4 files changed

+104
-71
lines changed

src/commands.ts

Lines changed: 95 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -253,42 +253,43 @@ export class SvnCommands {
253253
}
254254

255255
@command("svn.add")
256-
async addFile(resource: Resource) {
257-
const repository = this.model.getRepository(resource.resourceUri.fsPath);
256+
async addFile(
257+
...resourceStates: SourceControlResourceState[]
258+
): Promise<void> {
259+
const selection = this.getResourceStates(resourceStates);
258260

259-
if (!repository) {
261+
if (selection.length === 0) {
260262
return;
261263
}
262264

263-
try {
264-
await repository.addFile(resource.resourceUri.fsPath);
265-
} catch (error) {
266-
console.log(error);
267-
window.showErrorMessage("Unable to add file");
268-
}
265+
const uris = selection.map(resource => resource.resourceUri);
266+
267+
await this.runByRepository(uris, async (repository, resources) => {
268+
if (!repository) {
269+
return;
270+
}
271+
272+
const paths = resources.map(resource => resource.fsPath);
273+
274+
try {
275+
await repository.addFile(paths);
276+
} catch (error) {
277+
console.log(error);
278+
window.showErrorMessage("Unable to add file");
279+
}
280+
});
269281
}
270282

271283
@command("svn.addChangelist")
272284
async addChangelist(
273285
...resourceStates: SourceControlResourceState[]
274286
): Promise<void> {
275-
if (
276-
resourceStates.length === 0 ||
277-
!(resourceStates[0].resourceUri instanceof Uri)
278-
) {
279-
const resource = this.getSCMResource();
287+
const selection = this.getResourceStates(resourceStates);
280288

281-
if (!resource) {
282-
return;
283-
}
284-
285-
resourceStates = [resource];
289+
if (selection.length === 0) {
290+
return;
286291
}
287292

288-
const selection = resourceStates.filter(
289-
s => s instanceof Resource
290-
) as Resource[];
291-
292293
const uris = selection.map(resource => resource.resourceUri);
293294

294295
await this.runByRepository(uris, async (repository, resources) => {
@@ -345,23 +346,12 @@ export class SvnCommands {
345346
async removeChangelist(
346347
...resourceStates: SourceControlResourceState[]
347348
): Promise<void> {
348-
if (
349-
resourceStates.length === 0 ||
350-
!(resourceStates[0].resourceUri instanceof Uri)
351-
) {
352-
const resource = this.getSCMResource();
353-
354-
if (!resource) {
355-
return;
356-
}
349+
const selection = this.getResourceStates(resourceStates);
357350

358-
resourceStates = [resource];
351+
if (selection.length === 0) {
352+
return;
359353
}
360354

361-
const selection = resourceStates.filter(
362-
s => s instanceof Resource
363-
) as Resource[];
364-
365355
const uris = selection.map(resource => resource.resourceUri);
366356

367357
await this.runByRepository(uris, async (repository, resources) => {
@@ -742,8 +732,10 @@ export class SvnCommands {
742732
}
743733

744734
@command("svn.revert")
745-
async revert(...resourceStates: Resource[]) {
746-
if (resourceStates.length === 0) {
735+
async revert(...resourceStates: SourceControlResourceState[]): Promise<void> {
736+
const selection = this.getResourceStates(resourceStates);
737+
738+
if (selection.length === 0) {
747739
return;
748740
}
749741

@@ -757,18 +749,22 @@ export class SvnCommands {
757749
return;
758750
}
759751

760-
try {
761-
const paths = resourceStates.map(state => {
762-
return state.resourceUri;
763-
});
752+
const uris = selection.map(resource => resource.resourceUri);
764753

765-
await this.runByRepository(paths, async (repository, paths) =>
766-
repository.revert(paths)
767-
);
768-
} catch (error) {
769-
console.error(error);
770-
window.showErrorMessage("Unable to revert");
771-
}
754+
await this.runByRepository(uris, async (repository, resources) => {
755+
if (!repository) {
756+
return;
757+
}
758+
759+
const paths = resources.map(resource => resource.fsPath);
760+
761+
try {
762+
await repository.revert(paths);
763+
} catch (error) {
764+
console.log(error);
765+
window.showErrorMessage("Unable to revert");
766+
}
767+
});
772768
}
773769

774770
@command("svn.update", { repository: true })
@@ -799,12 +795,15 @@ export class SvnCommands {
799795
}
800796
}
801797

802-
@command("svn.remove", { repository: true })
803-
async remove(
804-
repository: Repository,
805-
...resourceStates: Resource[]
806-
): Promise<void> {
807-
let keepLocal;
798+
@command("svn.remove")
799+
async remove(...resourceStates: SourceControlResourceState[]): Promise<void> {
800+
const selection = this.getResourceStates(resourceStates);
801+
802+
if (selection.length === 0) {
803+
return;
804+
}
805+
806+
let keepLocal: boolean;
808807
const answer = await window.showWarningMessage(
809808
"Would you like to keep a local copy of the files?.",
810809
"Yes",
@@ -821,16 +820,22 @@ export class SvnCommands {
821820
keepLocal = false;
822821
}
823822

824-
try {
825-
const paths = resourceStates.map(state => {
826-
return state.resourceUri.fsPath;
827-
});
823+
const uris = selection.map(resource => resource.resourceUri);
828824

829-
const result = await repository.removeFiles(paths, keepLocal);
830-
} catch (error) {
831-
console.error(error);
832-
window.showErrorMessage("Unable to remove files");
833-
}
825+
await this.runByRepository(uris, async (repository, resources) => {
826+
if (!repository) {
827+
return;
828+
}
829+
830+
const paths = resources.map(resource => resource.fsPath);
831+
832+
try {
833+
const result = await repository.removeFiles(paths, keepLocal);
834+
} catch (error) {
835+
console.log(error);
836+
window.showErrorMessage("Unable to remove files");
837+
}
838+
});
834839
}
835840

836841
@command("svn.resolve", { repository: true })
@@ -868,7 +873,12 @@ export class SvnCommands {
868873
@command("svn.log", { repository: true })
869874
async log(repository: Repository) {
870875
try {
871-
const result = await repository.log();
876+
const logPromise = repository.log();
877+
window.withProgress(
878+
{ location: ProgressLocation.Window, title: "Fetching logs" },
879+
() => logPromise
880+
);
881+
const result = await logPromise;
872882
// send the log results to a new tab
873883
workspace.openTextDocument({ content: result }).then(doc => {
874884
window.showTextDocument(doc);
@@ -904,6 +914,25 @@ export class SvnCommands {
904914
}
905915
}
906916

917+
private getResourceStates(
918+
resourceStates: SourceControlResourceState[]
919+
): Resource[] {
920+
if (
921+
resourceStates.length === 0 ||
922+
!(resourceStates[0].resourceUri instanceof Uri)
923+
) {
924+
const resource = this.getSCMResource();
925+
926+
if (!resource) {
927+
return [];
928+
}
929+
930+
resourceStates = [resource];
931+
}
932+
933+
return resourceStates.filter(s => s instanceof Resource) as Resource[];
934+
}
935+
907936
private runByRepository<T>(
908937
resource: Uri,
909938
fn: (repository: Repository, resource: Uri) => Promise<T>

src/repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ export class Repository {
506506
});
507507
}
508508

509-
async addFile(filePath: string) {
509+
async addFile(filePath: string | string[]) {
510510
return await this.run(Operation.Add, () =>
511511
this.repository.addFile(filePath)
512512
);

src/svn.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,13 @@ export class Svn {
252252
return new Repository(this, repositoryRoot, workspaceRoot);
253253
}
254254

255-
add(path: string) {
256-
path = path.replace(/\\/g, "/");
257-
return this.exec("", ["add", path]);
255+
add(filePaths: string | string[]) {
256+
if (!Array.isArray(filePaths)) {
257+
filePaths = [filePaths];
258+
}
259+
260+
filePaths = filePaths.map(path => path.replace(/\\/g, "/"));
261+
return this.exec("", ["add", ...filePaths]);
258262
}
259263

260264
addChangelist(filePaths: string | string[], changelist: string) {

src/svnRepository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class Repository {
6767
return result.stdout;
6868
}
6969

70-
addFile(filePath: string) {
70+
addFile(filePath: string | string[]) {
7171
return this.svn.add(filePath);
7272
}
7373

0 commit comments

Comments
 (0)