-
Notifications
You must be signed in to change notification settings - Fork 35
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
marcellourbani
wants to merge
9
commits into
atlassian:main
Choose a base branch
from
marcellourbani:issue113-Add-hook-to-pretty-print-code-to-show-in-diff-in-atlascode
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
32bf7f3
Add external code formatter support - Fixes #5433
marcellourbani ec374ca
return api
marcellourbani 47592b2
allow async formatters
marcellourbani a5769a4
Corrupted diff when a file path includes unusual characters
marcellourbani dde702d
refactor context handling
marcellourbani d556aa9
Merge commit '17248c51d9aec1f51f83f2b7ade870e627bb7e71' into issue113…
marcellourbani 0a53e1b
Merge branch 'main' into issue113-Add-hook-to-pretty-print-code-to-sh…
marcellourbani 2e9bba1
fix unit tests
marcellourbani 45844e3
Merge branch 'main' into issue113-Add-hook-to-pretty-print-code-to-sh…
marcellourbani 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
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
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 |
---|---|---|
@@ -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); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.