Skip to content
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

Fixes #113 add hook to pretty print code to show in diff in atlascode #114

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,12 @@
},
"category": "Atlassian"
},
{
"command": "atlascode.bb.togglediffNormalize",
"title": "Normalize files",
"icon": "$(law)",
"category": "Atlassian"
},
{
"command": "atlascode.debug.bitbucketSites",
"title": "Show Bitbucket sites info",
Expand Down Expand Up @@ -480,6 +486,11 @@
"command": "atlascode.bb.editThisFile",
"when": "resourceScheme == atlascode.bbpr && config.atlascode.bitbucket.enabled",
"group": "navigation"
},
{
"command": "atlascode.bb.togglediffNormalize",
"group": "navigation",
"when": "isInDiffEditor && resourceScheme == atlascode.bbpr && atlascode:IsNormalizerEnabled"
}
],
"editor/context": [
Expand Down Expand Up @@ -811,6 +822,10 @@
"command": "atlascode.bb.toggleCommentsVisibility",
"when": "false"
},
{
"command": "atlascode.bb.togglediffNormalize",
"when": "false"
},
{
"command": "atlascode.jira.showIssueForKey",
"when": "atlascode:isJiraAuthenticated && config.atlascode.jira.enabled"
Expand Down Expand Up @@ -1575,4 +1590,4 @@
"webpack-node-externals": "^3.0.0"
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
}
3 changes: 3 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { ConfigSection, ConfigSubSection } from './lib/ipc/models/config';
import { AbstractBaseNode } from './views/nodes/abstractBaseNode';
import { IssueNode } from './views/nodes/issueNode';
import { PipelineNode } from './views/pipelines/PipelinesTree';
import { toggleDiffNormalize } from './normalize';

export enum Commands {
BitbucketSelectContainer = 'atlascode.bb.selectContainer',
Expand Down Expand Up @@ -51,6 +52,7 @@ export enum Commands {
BitbucketMarkTaskComplete = 'atlascode.bb.markTaskComplete',
BitbucketMarkTaskIncomplete = 'atlascode.bb.markTaskIncomplete',
BitbucketToggleCommentsVisibility = 'atlascode.bb.toggleCommentsVisibility',
BitbucketToggleDiffNormalize = 'atlascode.bb.togglediffNormalize',
EditThisFile = 'atlascode.bb.editThisFile',
CreateIssue = 'atlascode.jira.createIssue',
RefreshJiraExplorer = 'atlascode.jira.refreshExplorer',
Expand Down Expand Up @@ -199,6 +201,7 @@ export function registerCommands(vscodeContext: ExtensionContext) {
diffArgs[0]();
commands.executeCommand('vscode.diff', ...diffArgs.slice(1));
}),
commands.registerCommand(Commands.BitbucketToggleDiffNormalize, toggleDiffNormalize),
commands.registerCommand(Commands.RerunPipeline, (node: PipelineNode) => {
rerunPipeline(node.pipeline);
}),
Expand Down
12 changes: 12 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ export const AuthInfoVersionKey = 'authInfoVersion';

export const ATLASCODE_TEST_USER_EMAIL = '[email protected]';
export const ATLASCODE_TEST_HOST = 'axontest2025.atlassian.net';
export enum CommandContext {
JiraExplorer = 'atlascode:jiraExplorerEnabled',
BitbucketExplorer = 'atlascode:bitbucketExplorerEnabled',
PipelineExplorer = 'atlascode:pipelineExplorerEnabled',
BitbucketIssuesExplorer = 'atlascode:bitbucketIssuesExplorerEnabled',
OpenIssuesTree = 'atlascode:openIssuesTreeEnabled',
AssignedIssuesTree = 'atlascode:assignedIssuesTreeEnabled',
JiraLoginTree = 'atlascode:jiraLoginTreeEnabled',
IsJiraAuthenticated = 'atlascode:isJiraAuthenticated',
IsBBAuthenticated = 'atlascode:isBBAuthenticated',
IsNormalizerEnabled = 'atlascode:IsNormalizerEnabled',
}
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { registerResources } from './resources';
import { GitExtension } from './typings/git';
import { pid } from 'process';
import { startListening } from './atlclients/negotiate';
import { api } from './normalize';

const AnalyticDelay = 5000;

Expand Down Expand Up @@ -86,6 +87,7 @@ export async function activate(context: ExtensionContext) {
duration[0] * 1000 + Math.floor(duration[1] / 1000000)
} ms`,
);
return api;
}

async function activateBitbucketFeatures() {
Expand Down
72 changes: 72 additions & 0 deletions src/normalize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { commands, TabInputTextDiff, TextEditor, Uri, window } from 'vscode';
import { CommandContext } from './constants';
import { Container } from './container';

interface CodeNormalizer {
isRelevant: (u: Uri) => boolean;
normalize: (code: string, uri: Uri) => string | Promise<string>;
}

const normalizers: CodeNormalizer[] = [];

function setCommandContext(key: CommandContext | string, value: any) {
return commands.executeCommand('setContext', key, value);
}

const registerCodeNormalizer = (normalizer: CodeNormalizer) => {
if (normalizers.indexOf(normalizer) < 0) {
normalizers.push(normalizer);
}
return {
dispose: () => {
const idx = normalizers.indexOf(normalizer);
if (idx >= 0) {
normalizers.splice(idx, 1);
}
},
};
};

const enableDiffNormalize = (e?: TextEditor) => {
const relevant = e?.document.uri.scheme === 'atlascode.bbpr';
const active = relevant && normalizers.some((n) => n.isRelevant(e.document.uri));
setCommandContext(CommandContext.IsNormalizerEnabled, active);
};

const toggleNorm = (u: Uri) => {
const q = JSON.parse(u.query);
if (q.normalized) {
delete q.normalized;
} else {
q.normalized = true;
}
return u.with({ query: JSON.stringify(q) });
};

export const toggleDiffNormalize = () => {
try {
const tab = window.tabGroups.activeTabGroup.activeTab;
if (tab?.input instanceof TabInputTextDiff) {
const { original, modified } = tab.input;
const controller = Container.bitbucketContext.prCommentController;
const origN = toggleNorm(original);
const modifN = toggleNorm(modified);
controller.provideComments(origN);
controller.provideComments(modifN);
return commands.executeCommand<void>('vscode.diff', origN, modifN, tab.label);
}
} catch (error) {}
return;
};

export const normalize = (original: string, uri: Uri) => {
for (const n of normalizers) {
if (n.isRelevant(uri)) {
return n.normalize(original, uri);
}
}
return original;
};

export const api = { registerCodeNormalizer };
window.onDidChangeActiveTextEditor(enableDiffNormalize);
3 changes: 2 additions & 1 deletion src/views/gitContentProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BitbucketContext } from '../bitbucket/bbContext';
import { clientForSite } from '../bitbucket/bbUtils';
import { Container } from '../container';
import { PRFileDiffQueryParams } from './pullrequest/diffViewHelper';
import { normalize } from 'src/normalize';

//This class is responsible for fetching the text of a specific version of a file which may not be on your machine
//Everytime the vscode.diff is invoked in this extension, it's using this file to fetch the data for both files.
Expand Down Expand Up @@ -58,6 +59,6 @@ export class GitContentProvider implements vscode.TextDocumentContentProvider {
);
}

return content || '';
return normalize(content || '', uri);
}
}