Browse Source

Hides commands not available in virtual workspaces

main
Eric Amodio 2 years ago
parent
commit
445edcc093
8 changed files with 279 additions and 213 deletions
  1. +187
    -183
      package.json
  2. +11
    -1
      src/commands/git/stash.ts
  3. +37
    -18
      src/commands/gitCommands.utils.ts
  4. +2
    -0
      src/constants.ts
  5. +7
    -5
      src/context.ts
  6. +2
    -2
      src/extension.ts
  7. +5
    -1
      src/git/gitProviderService.ts
  8. +28
    -3
      src/premium/github/githubGitProvider.ts

+ 187
- 183
package.json
File diff suppressed because it is too large
View File


+ 11
- 1
src/commands/git/stash.ts View File

@ -1,6 +1,7 @@
import { QuickInputButtons, QuickPickItem, Uri, window } from 'vscode'; import { QuickInputButtons, QuickPickItem, Uri, window } from 'vscode';
import { GlyphChars } from '../../constants';
import { ContextKeys, GlyphChars } from '../../constants';
import { Container } from '../../container'; import { Container } from '../../container';
import { getContext } from '../../context';
import { StashApplyError, StashApplyErrorReason } from '../../git/errors'; import { StashApplyError, StashApplyErrorReason } from '../../git/errors';
import { GitReference, GitStashCommit, GitStashReference, Repository } from '../../git/models'; import { GitReference, GitStashCommit, GitStashReference, Repository } from '../../git/models';
import { Logger } from '../../logger'; import { Logger } from '../../logger';
@ -31,6 +32,7 @@ import {
interface Context { interface Context {
repos: Repository[]; repos: Repository[];
associatedView: ViewsWithRepositoryFolders; associatedView: ViewsWithRepositoryFolders;
readonly: boolean;
title: string; title: string;
} }
@ -150,6 +152,10 @@ export class StashGitCommand extends QuickCommand {
const context: Context = { const context: Context = {
repos: this.container.git.openRepositories, repos: this.container.git.openRepositories,
associatedView: this.container.stashesView, associatedView: this.container.stashesView,
readonly:
getContext<boolean>(ContextKeys.Readonly, false) ||
getContext<boolean>(ContextKeys.Untrusted, false) ||
getContext<boolean>(ContextKeys.HasVirtualFolders, false),
title: this.title, title: this.title,
}; };
@ -158,6 +164,10 @@ export class StashGitCommand extends QuickCommand {
while (this.canStepsContinue(state)) { while (this.canStepsContinue(state)) {
context.title = this.title; context.title = this.title;
if (context.readonly) {
state.subcommand = 'list';
}
if (state.counter < 1 || state.subcommand == null) { if (state.counter < 1 || state.subcommand == null) {
this.subcommand = undefined; this.subcommand = undefined;

+ 37
- 18
src/commands/gitCommands.utils.ts View File

@ -1,5 +1,7 @@
import { GitCommandSorting } from '../config'; import { GitCommandSorting } from '../config';
import { ContextKeys } from '../constants';
import type { Container } from '../container'; import type { Container } from '../container';
import { getContext } from '../context';
import { Usage, WorkspaceState } from '../storage'; import { Usage, WorkspaceState } from '../storage';
import { BranchGitCommand } from './git/branch'; import { BranchGitCommand } from './git/branch';
import { CherryPickGitCommand } from './git/cherry-pick'; import { CherryPickGitCommand } from './git/cherry-pick';
@ -50,28 +52,45 @@ export class PickCommandStep implements QuickPickStep {
readonly title = 'GitLens'; readonly title = 'GitLens';
constructor(private readonly container: Container, args?: GitCommandsCommandArgs) { constructor(private readonly container: Container, args?: GitCommandsCommandArgs) {
const hasVirtualFolders = getContext<boolean>(ContextKeys.HasVirtualFolders, false);
const readonly =
hasVirtualFolders ||
getContext<boolean>(ContextKeys.Readonly, false) ||
getContext<boolean>(ContextKeys.Untrusted, false);
this.items = [ this.items = [
new BranchGitCommand(container, args?.command === 'branch' ? args : undefined),
new CherryPickGitCommand(container, args?.command === 'cherry-pick' ? args : undefined),
new CoAuthorsGitCommand(container, args?.command === 'co-authors' ? args : undefined),
new FetchGitCommand(container, args?.command === 'fetch' ? args : undefined),
readonly ? undefined : new BranchGitCommand(container, args?.command === 'branch' ? args : undefined),
readonly
? undefined
: new CherryPickGitCommand(container, args?.command === 'cherry-pick' ? args : undefined),
hasVirtualFolders
? undefined
: new CoAuthorsGitCommand(container, args?.command === 'co-authors' ? args : undefined),
readonly ? undefined : new FetchGitCommand(container, args?.command === 'fetch' ? args : undefined),
new LogGitCommand(container, args?.command === 'log' ? args : undefined), new LogGitCommand(container, args?.command === 'log' ? args : undefined),
new MergeGitCommand(container, args?.command === 'merge' ? args : undefined),
new PullGitCommand(container, args?.command === 'pull' ? args : undefined),
new PushGitCommand(container, args?.command === 'push' ? args : undefined),
new RebaseGitCommand(container, args?.command === 'rebase' ? args : undefined),
new ResetGitCommand(container, args?.command === 'reset' ? args : undefined),
new RevertGitCommand(container, args?.command === 'revert' ? args : undefined),
readonly ? undefined : new MergeGitCommand(container, args?.command === 'merge' ? args : undefined),
readonly ? undefined : new PullGitCommand(container, args?.command === 'pull' ? args : undefined),
readonly ? undefined : new PushGitCommand(container, args?.command === 'push' ? args : undefined),
readonly ? undefined : new RebaseGitCommand(container, args?.command === 'rebase' ? args : undefined),
readonly ? undefined : new ResetGitCommand(container, args?.command === 'reset' ? args : undefined),
readonly ? undefined : new RevertGitCommand(container, args?.command === 'revert' ? args : undefined),
new SearchGitCommand(container, args?.command === 'search' || args?.command === 'grep' ? args : undefined), new SearchGitCommand(container, args?.command === 'search' || args?.command === 'grep' ? args : undefined),
new ShowGitCommand(container, args?.command === 'show' ? args : undefined), new ShowGitCommand(container, args?.command === 'show' ? args : undefined),
new StashGitCommand(container, args?.command === 'stash' ? args : undefined),
new StatusGitCommand(container, args?.command === 'status' ? args : undefined),
new SwitchGitCommand(
container,
args?.command === 'switch' || args?.command === 'checkout' ? args : undefined,
),
new TagGitCommand(container, args?.command === 'tag' ? args : undefined),
];
hasVirtualFolders
? undefined
: new StashGitCommand(container, args?.command === 'stash' ? args : undefined),
hasVirtualFolders
? undefined
: new StatusGitCommand(container, args?.command === 'status' ? args : undefined),
readonly
? undefined
: new SwitchGitCommand(
container,
args?.command === 'switch' || args?.command === 'checkout' ? args : undefined,
),
readonly ? undefined : new TagGitCommand(container, args?.command === 'tag' ? args : undefined),
].filter(<T>(i: T | undefined): i is T => i != null);
if (this.container.config.gitCommands.sortBy === GitCommandSorting.Usage) { if (this.container.config.gitCommands.sortBy === GitCommandSorting.Usage) {
const usage = this.container.storage.getWorkspace<Usage>(WorkspaceState.GitCommandPaletteUsage); const usage = this.container.storage.getWorkspace<Usage>(WorkspaceState.GitCommandPaletteUsage);

+ 2
- 0
src/constants.ts View File

@ -242,7 +242,9 @@ export const enum ContextKeys {
HasConnectedRemotes = 'gitlens:hasConnectedRemotes', HasConnectedRemotes = 'gitlens:hasConnectedRemotes',
HasRemotes = 'gitlens:hasRemotes', HasRemotes = 'gitlens:hasRemotes',
HasRichRemotes = 'gitlens:hasRichRemotes', HasRichRemotes = 'gitlens:hasRichRemotes',
HasVirtualFolders = 'gitlens:hasVirtualFolders',
Readonly = 'gitlens:readonly', Readonly = 'gitlens:readonly',
Untrusted = 'gitlens:untrusted',
ViewsCanCompare = 'gitlens:views:canCompare', ViewsCanCompare = 'gitlens:views:canCompare',
ViewsCanCompareFile = 'gitlens:views:canCompare:file', ViewsCanCompareFile = 'gitlens:views:canCompare:file',
ViewsCommitsMyCommitsOnly = 'gitlens:views:commits:myCommitsOnly', ViewsCommitsMyCommitsOnly = 'gitlens:views:commits:myCommitsOnly',

+ 7
- 5
src/context.ts View File

@ -1,16 +1,18 @@
import { commands } from 'vscode'; import { commands } from 'vscode';
import { ContextKeys, CoreCommands } from './constants'; import { ContextKeys, CoreCommands } from './constants';
// const contextStorage = new Map<string, unknown>();
const contextStorage = new Map<string, unknown>();
// export function getContext(key: ContextKeys): unknown | undefined {
// return contextStorage.get(key);
// }
export function getContext<T>(key: ContextKeys): T | undefined;
export function getContext<T>(key: ContextKeys, defaultValue: T): T;
export function getContext<T>(key: ContextKeys, defaultValue?: T): T | undefined {
return (contextStorage.get(key) as T | undefined) ?? defaultValue;
}
export async function setContext( export async function setContext(
key: ContextKeys | `${ContextKeys.ActionPrefix}${string}` | `${ContextKeys.KeyPrefix}${string}`, key: ContextKeys | `${ContextKeys.ActionPrefix}${string}` | `${ContextKeys.KeyPrefix}${string}`,
value: unknown, value: unknown,
): Promise<void> { ): Promise<void> {
// contextStorage.set(key, value);
contextStorage.set(key, value);
void (await commands.executeCommand(CoreCommands.SetContext, key, value)); void (await commands.executeCommand(CoreCommands.SetContext, key, value));
} }

+ 2
- 2
src/extension.ts View File

@ -60,9 +60,9 @@ export async function activate(context: ExtensionContext): Promise
} }
if (!workspace.isTrusted) { if (!workspace.isTrusted) {
void setContext(ContextKeys.Readonly, true);
void setContext(ContextKeys.Untrusted, true);
context.subscriptions.push( context.subscriptions.push(
workspace.onDidGrantWorkspaceTrust(() => void setContext(ContextKeys.Readonly, undefined)),
workspace.onDidGrantWorkspaceTrust(() => void setContext(ContextKeys.Untrusted, undefined)),
); );
} }

+ 5
- 1
src/git/gitProviderService.ts View File

@ -27,7 +27,7 @@ import { WorkspaceState } from '../storage';
import { groupByFilterMap, groupByMap } from '../system/array'; import { groupByFilterMap, groupByMap } from '../system/array';
import { gate } from '../system/decorators/gate'; import { gate } from '../system/decorators/gate';
import { debug, log } from '../system/decorators/log'; import { debug, log } from '../system/decorators/log';
import { count, filter, first, flatMap, map } from '../system/iterable';
import { count, filter, first, flatMap, map, some } from '../system/iterable';
import { dirname, getBestPath, getScheme, isAbsolute, maybeUri, normalizePath } from '../system/path'; import { dirname, getBestPath, getScheme, isAbsolute, maybeUri, normalizePath } from '../system/path';
import { cancellable, isPromise, PromiseCancelledError } from '../system/promise'; import { cancellable, isPromise, PromiseCancelledError } from '../system/promise';
import { VisitedPathsTrie } from '../system/trie'; import { VisitedPathsTrie } from '../system/trie';
@ -427,6 +427,10 @@ export class GitProviderService implements Disposable {
return groupByMap(repositories, r => r.provider.id); return groupByMap(repositories, r => r.provider.id);
} }
hasOpenRepositories(id: GitProviderId): boolean {
return some(this.repositories, r => !r.closed && (id == null || id === r.provider.id));
}
private _discoveredWorkspaceFolders = new Map<WorkspaceFolder, Promise<Repository[]>>(); private _discoveredWorkspaceFolders = new Map<WorkspaceFolder, Promise<Repository[]>>();
@log<GitProviderService['discoverRepositories']>({ args: { 0: folders => folders.length } }) @log<GitProviderService['discoverRepositories']>({ args: { 0: folders => folders.length } })

+ 28
- 3
src/premium/github/githubGitProvider.ts View File

@ -15,8 +15,9 @@ import {
} from 'vscode'; } from 'vscode';
import { encodeUtf8Hex } from '@env/hex'; import { encodeUtf8Hex } from '@env/hex';
import { configuration } from '../../configuration'; import { configuration } from '../../configuration';
import { CharCode, Schemes } from '../../constants';
import { CharCode, ContextKeys, Schemes } from '../../constants';
import type { Container } from '../../container'; import type { Container } from '../../container';
import { setContext } from '../../context';
import { import {
AuthenticationError, AuthenticationError,
AuthenticationErrorReason, AuthenticationErrorReason,
@ -156,6 +157,10 @@ export class GitHubGitProvider implements GitProvider, Disposable {
} }
} }
updateContext(): void {
void setContext(ContextKeys.HasVirtualFolders, this.container.git.hasOpenRepositories(this.descriptor.id));
}
openRepository( openRepository(
folder: WorkspaceFolder | undefined, folder: WorkspaceFolder | undefined,
uri: Uri, uri: Uri,
@ -1334,10 +1339,30 @@ export class GitHubGitProvider implements GitProvider, Disposable {
@log() @log()
async getLogForSearch( async getLogForSearch(
_repoPath: string,
_search: SearchPattern,
repoPath: string,
search: SearchPattern,
_options?: { limit?: number; ordering?: string | null; skip?: number }, _options?: { limit?: number; ordering?: string | null; skip?: number },
): Promise<GitLog | undefined> { ): Promise<GitLog | undefined> {
if (repoPath == null) return undefined;
const operations = SearchPattern.parseSearchOperations(search.pattern);
const values = operations.get('commit:');
if (values != null) {
const commit = await this.getCommit(repoPath, values[0]);
if (commit == null) return undefined;
return {
repoPath: repoPath,
commits: new Map([[commit.sha, commit]]),
sha: commit.sha,
range: undefined,
count: 1,
limit: 1,
hasMore: false,
};
}
// TODO@eamodio try implementing with the commit search api // TODO@eamodio try implementing with the commit search api
return undefined; return undefined;
} }

Loading…
Cancel
Save