Skip to content

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

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
wants to merge 9 commits into
base: main
Choose a base branch
from
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,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 @@ -464,6 +470,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 @@ -809,6 +820,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
1 change: 1 addition & 0 deletions src/commandContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export enum CommandContext {
JiraLoginTree = 'atlascode:jiraLoginTreeEnabled',
IsJiraAuthenticated = 'atlascode:isJiraAuthenticated',
IsBBAuthenticated = 'atlascode:isBBAuthenticated',
IsNormalizerEnabled = 'atlascode:IsNormalizerEnabled',
}

export function setCommandContext(key: CommandContext | string, value: any) {
Expand Down
2 changes: 2 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { transitionIssue } from './jira/transitionIssue';
import { knownLinkIdMap } from './lib/ipc/models/common';
import { ConfigSection, ConfigSubSection } from './lib/ipc/models/config';
import { Logger } from './logger';
import { toggleDiffNormalize } from './normalize';
import { AbstractBaseNode } from './views/nodes/abstractBaseNode';
import { IssueNode } from './views/nodes/issueNode';
import { PipelineNode } from './views/pipelines/PipelinesTree';
Expand Down Expand Up @@ -174,6 +175,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
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const 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',
RefreshAssignedWorkItemsExplorer = 'atlascode.jira.refreshAssignedWorkItemsExplorer',
Expand Down
4 changes: 3 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Container } from './container';
import { registerAnalyticsClient, registerErrorReporting, unregisterErrorReporting } from './errorReporting';
import { provideCodeLenses } from './jira/todoObserver';
import { Logger } from './logger';
import { api } from './normalize';
import { PipelinesYamlCompletionProvider } from './pipelines/yaml/pipelinesYamlCompletionProvider';
import {
activateYamlExtension,
Expand Down Expand Up @@ -46,7 +47,7 @@ export async function activate(context: ExtensionContext) {
context.globalState.update('rulingPid', pid);

try {
await Container.initialize(context, configuration.get<IConfig>(), atlascodeVersion);
Container.initialize(context, configuration.get<IConfig>(), atlascodeVersion);

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

function activateErrorReporting(): void {
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, setCommandContext } from './commandContext';
import { Container } from './container';
import { Logger } from './logger';

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

const normalizers: CodeNormalizer[] = [];

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) {
Logger.debug('[Failed to toggle normalizer]', 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 @@ -4,6 +4,7 @@ import vscode from 'vscode';
import { BitbucketContext } from '../bitbucket/bbContext';
import { clientForSite } from '../bitbucket/bbUtils';
import { Container } from '../container';
import { normalize } from '../normalize';
import { PRFileDiffQueryParams } from './pullrequest/diffViewHelper';

//This class is responsible for fetching the text of a specific version of a file which may not be on your machine
Expand Down Expand Up @@ -58,6 +59,6 @@ export class GitContentProvider implements vscode.TextDocumentContentProvider {
);
}

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