-
Notifications
You must be signed in to change notification settings - Fork 24
Added checkboxes for file reviewing #340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sn2912
wants to merge
2
commits into
main
Choose a base branch
from
add-checkbox-content-based-hashing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+345
−58
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -12,16 +12,46 @@ import { DescriptionNode, PullRequestTitlesNode } from './pullrequest/pullReques | |
import { PullRequestNodeDataProvider } from './pullRequestNodeDataProvider'; | ||
import { RefreshTimer } from './RefreshTimer'; | ||
import { BitbucketActivityMonitor } from './BitbucketActivityMonitor'; | ||
import { PullRequestFilesNode } from './nodes/pullRequestFilesNode'; | ||
import { DirectoryNode } from './nodes/directoryNode'; | ||
import { TreeView } from 'vscode'; | ||
import { AbstractBaseNode } from './nodes/abstractBaseNode'; | ||
|
||
export abstract class BitbucketExplorer extends Explorer implements Disposable { | ||
private _disposable: Disposable; | ||
|
||
private monitor: BitbucketActivityMonitor | undefined; | ||
private _refreshTimer: RefreshTimer; | ||
private _onDidChangeTreeData = new vscode.EventEmitter<AbstractBaseNode | undefined>(); | ||
|
||
protected newTreeView(): TreeView<AbstractBaseNode> | undefined { | ||
super.newTreeView(); | ||
this.setupCheckboxHandling(); | ||
return this.treeView; | ||
} | ||
|
||
private setupCheckboxHandling(): void { | ||
if (!this.treeView) { | ||
return; | ||
} | ||
this.treeView.onDidChangeCheckboxState((event) => { | ||
event.items.forEach(([item, state]) => { | ||
const checked = state === vscode.TreeItemCheckboxState.Checked; | ||
if (item instanceof PullRequestFilesNode || item instanceof DirectoryNode) { | ||
item.checked = checked; | ||
this._onDidChangeTreeData.fire(item); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
constructor(protected ctx: BitbucketContext) { | ||
super(() => this.dispose()); | ||
|
||
setTimeout(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why> |
||
this.setupCheckboxHandling(); | ||
}, 50); | ||
|
||
Container.context.subscriptions.push(configuration.onDidChange(this._onConfigurationChanged, this)); | ||
|
||
this._refreshTimer = new RefreshTimer(this.explorerEnabledConfiguration(), this.refreshConfiguration(), () => | ||
|
This file contains hidden or 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,21 @@ | ||
import * as vscode from 'vscode'; | ||
import { AbstractBaseNode } from './abstractBaseNode'; | ||
|
||
export class FilesRootNode extends AbstractBaseNode { | ||
constructor( | ||
private fileNodes: AbstractBaseNode[], | ||
parent?: AbstractBaseNode, | ||
) { | ||
super(parent); | ||
} | ||
|
||
getTreeItem(): vscode.TreeItem { | ||
const item = new vscode.TreeItem('Files', vscode.TreeItemCollapsibleState.Collapsed); | ||
item.contextValue = 'files-root'; | ||
return item; | ||
} | ||
|
||
async getChildren(): Promise<AbstractBaseNode[]> { | ||
return this.fileNodes; | ||
} | ||
} |
This file contains hidden or 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,64 @@ | ||
import { Logger } from 'src/logger'; | ||
import vscode from 'vscode'; | ||
interface CheckboxState { | ||
id: string; | ||
timestamp: number; | ||
} | ||
export class CheckboxStateManager { | ||
private readonly STATE_EXPIRY = 24 * 60 * 60 * 1000 * 30; // 30 days | ||
private readonly STORAGE_KEY = 'bitbucket.viewedFiles'; | ||
|
||
constructor(private context: vscode.ExtensionContext) { | ||
this.cleanup(); | ||
} | ||
|
||
private async cleanup(): Promise<void> { | ||
const states = this.context.workspaceState.get<CheckboxState[]>(this.STORAGE_KEY, []); | ||
const now = Date.now(); | ||
|
||
const validStates = states.filter((state) => { | ||
const isValid = now - state.timestamp < this.STATE_EXPIRY; | ||
if (!isValid) { | ||
Logger.debug(`Removing expired checkbox state: ${state.id}`); | ||
} | ||
return isValid; | ||
}); | ||
|
||
await this.context.workspaceState.update(this.STORAGE_KEY, validStates); | ||
Logger.debug(`Cleanup complete. Remaining states: ${validStates.length}`); | ||
} | ||
|
||
isChecked(id: string): boolean { | ||
const states = this.context.workspaceState.get<CheckboxState[]>(this.STORAGE_KEY, []); | ||
const state = states.find((state) => state.id === id); | ||
|
||
if (state) { | ||
if (Date.now() - state.timestamp >= this.STATE_EXPIRY) { | ||
this.setChecked(id, false); | ||
return false; | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
setChecked(id: string, checked: boolean): void { | ||
const states = this.context.workspaceState.get<CheckboxState[]>(this.STORAGE_KEY, []); | ||
|
||
if (checked) { | ||
const existingIndex = states.findIndex((state) => state.id === id); | ||
if (existingIndex !== -1) { | ||
states.splice(existingIndex, 1); | ||
} | ||
states.push({ id, timestamp: Date.now() }); | ||
} else { | ||
const index = states.findIndex((state) => state.id === id); | ||
if (index !== -1) { | ||
states.splice(index, 1); | ||
} | ||
} | ||
|
||
this.context.workspaceState.update(this.STORAGE_KEY, states); | ||
Logger.debug(`Checkbox state updated: ${id} = ${checked}`); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -2,27 +2,125 @@ import * as vscode from 'vscode'; | |
import { PRDirectory } from '../pullrequest/diffViewHelper'; | ||
import { AbstractBaseNode } from './abstractBaseNode'; | ||
import { PullRequestFilesNode } from './pullRequestFilesNode'; | ||
import { Container } from 'src/container'; | ||
import { PullRequest } from 'src/bitbucket/model'; | ||
import * as crypto from 'crypto'; | ||
|
||
export class DirectoryNode extends AbstractBaseNode { | ||
constructor(private directoryData: PRDirectory) { | ||
isRootFilesDirectory: boolean | undefined; | ||
constructor( | ||
private directoryData: PRDirectory, | ||
private section: 'files' | 'commits' = 'files', | ||
private pr: PullRequest, | ||
private commitHash?: string, | ||
) { | ||
super(); | ||
} | ||
|
||
private _isDirectClick = false; | ||
|
||
get directoryId(): string { | ||
const prUrl = this.pr.data.url; | ||
const prUrlPath = vscode.Uri.parse(prUrl).path; | ||
const prId = prUrlPath.slice(prUrlPath.lastIndexOf('/') + 1); | ||
const repoUrl = prUrl.slice(0, prUrl.indexOf('/pull-requests')); | ||
const repoId = repoUrl.slice(repoUrl.lastIndexOf('/') + 1); | ||
const dirPath = this.directoryData.dirPath; | ||
|
||
const dirId = | ||
this.section === 'commits' | ||
? `repo-${repoId}-pr-${prId}-section-${this.section}-commit-${this.commitHash}-directory-${dirPath}` | ||
: `repo-${repoId}-pr-${prId}-section-${this.section}-directory-${dirPath}`; | ||
return crypto.createHash('md5').update(dirId).digest('hex'); | ||
} | ||
|
||
private areAllChildrenChecked(): boolean { | ||
const allFilesChecked = this.directoryData.files.every((file) => { | ||
const fileNode = new PullRequestFilesNode(file, this.section, this.pr); | ||
return fileNode.checked; | ||
}); | ||
|
||
const allSubdirsChecked = Array.from(this.directoryData.subdirs.values()).every((subdir) => { | ||
const subdirNode = new DirectoryNode(subdir, this.section, this.pr); | ||
return subdirNode.checked; | ||
}); | ||
|
||
return allFilesChecked && allSubdirsChecked; | ||
} | ||
|
||
set checked(value: boolean) { | ||
// This is to avoid infinite loops when setting the checked state | ||
if (this._isDirectClick) { | ||
return; | ||
} | ||
|
||
try { | ||
this._isDirectClick = true; | ||
Container.checkboxStateManager.setChecked(this.directoryId, value); | ||
|
||
this.directoryData.files.forEach((file) => { | ||
const fileNode = new PullRequestFilesNode(file, this.section, this.pr); | ||
Container.checkboxStateManager.setChecked(fileNode.fileId, value); | ||
}); | ||
this.directoryData.subdirs.forEach((subdir) => { | ||
const subdirNode = new DirectoryNode(subdir, this.section, this.pr); | ||
Container.checkboxStateManager.setChecked(subdirNode.directoryId, value); | ||
}); | ||
} finally { | ||
this._isDirectClick = false; | ||
} | ||
} | ||
|
||
get checked(): boolean { | ||
const hasExplicitState = Container.checkboxStateManager.isChecked(this.directoryId); | ||
if (!hasExplicitState) { | ||
return this.areAllChildrenChecked(); | ||
} | ||
return hasExplicitState; | ||
} | ||
|
||
async getTreeItem(): Promise<vscode.TreeItem> { | ||
const item = new vscode.TreeItem(this.directoryData.name, vscode.TreeItemCollapsibleState.Expanded); | ||
this.isRootFilesDirectory = | ||
this.section === 'files' && this.directoryData.name === 'Files' && this.directoryData.dirPath === ''; | ||
|
||
const item = new vscode.TreeItem( | ||
this.directoryData.name, | ||
this.isRootFilesDirectory | ||
? vscode.TreeItemCollapsibleState.Collapsed | ||
: vscode.TreeItemCollapsibleState.Expanded, | ||
); | ||
item.tooltip = this.directoryData.name; | ||
item.iconPath = vscode.ThemeIcon.Folder; | ||
|
||
if (!this.isRootFilesDirectory) { | ||
item.iconPath = vscode.ThemeIcon.Folder; | ||
} | ||
|
||
const allChecked = this.areAllChildrenChecked(); | ||
|
||
if (!this.isRootFilesDirectory) { | ||
item.checkboxState = this.checked | ||
? vscode.TreeItemCheckboxState.Checked | ||
: vscode.TreeItemCheckboxState.Unchecked; | ||
item.contextValue = `directory${allChecked ? '.checked' : ''}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? |
||
} | ||
|
||
if (!this.isRootFilesDirectory) { | ||
item.id = this.directoryId; | ||
} | ||
|
||
return item; | ||
} | ||
|
||
async getChildren(element?: AbstractBaseNode): Promise<AbstractBaseNode[]> { | ||
async getChildren(): Promise<AbstractBaseNode[]> { | ||
const fileNodes: AbstractBaseNode[] = this.directoryData.files.map( | ||
(diffViewArg) => new PullRequestFilesNode(diffViewArg, this.section, this.pr), | ||
); | ||
|
||
const directoryNodes: DirectoryNode[] = Array.from( | ||
this.directoryData.subdirs.values(), | ||
(subdir) => new DirectoryNode(subdir), | ||
(subdir) => new DirectoryNode(subdir, this.section, this.pr), | ||
); | ||
const fileNodes: AbstractBaseNode[] = this.directoryData.files.map( | ||
(diffViewArg) => new PullRequestFilesNode(diffViewArg), | ||
); | ||
return fileNodes.concat(directoryNodes); | ||
|
||
return [...directoryNodes, ...fileNodes]; | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this to pr explorer section