-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Lars Hvam <larshp@hotmail.com>
- Loading branch information
1 parent
2fa365a
commit 8af6299
Showing
8 changed files
with
357 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -23,4 +23,4 @@ | |
"@types/vscode": "^1.91.0", | ||
"buffer": "^6.0.3" | ||
} | ||
} | ||
} |
This file contains 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 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,83 @@ | ||
import {extensions, Uri, workspace} from "vscode"; | ||
import {BaseLanguageClient} from "vscode-languageclient"; | ||
|
||
export const ATLASCODEDIFF = "atlascode.bbpr"; | ||
export interface CodeNormalizer { | ||
isRelevant: (u: Uri) => boolean; | ||
normalize: (code: string, uri: Uri) => Promise<string>; | ||
} | ||
|
||
export let integrationIsActive:(u:Uri) => boolean = () => false; | ||
interface BitBucketApi { | ||
registerCodeNormalizer:(n:CodeNormalizer)=>Disposable; | ||
} | ||
|
||
const shouldNormalize = (u:Uri) => { | ||
try { | ||
const o = JSON.parse(u.query); | ||
return !! o.normalized; | ||
} catch (error) { | ||
return false; | ||
} | ||
}; | ||
|
||
const extractname = (u:Uri) => { | ||
if (u.scheme !== ATLASCODEDIFF) {return u.path;} | ||
try { | ||
const details = JSON.parse(u.query); | ||
if (details.path && typeof details.path === "string") { | ||
return details.path; | ||
} | ||
} catch (error) { | ||
return u.fragment; | ||
} | ||
}; | ||
const normalizations = ["On by default", "Off by default", "deactivated"] as const; | ||
type Normalization<I extends number> = (typeof normalizations[I]); | ||
const getNormalization = <I extends number>(): Normalization<I> => { | ||
const n = workspace.getConfiguration("abaplint").get("codeNormalization") as any; | ||
if (normalizations.includes(n)) {return n;} | ||
return "Off by default"; | ||
}; | ||
const isAbap = (u:Uri) => !!(u.fsPath.match(/\.abap$/) || u.fragment.match(/\.abap$/)); | ||
export const getAbapCodeNormalizer = (client: BaseLanguageClient):CodeNormalizer => { | ||
const normalization = getNormalization(); | ||
const inverted = normalization === "On by default"; | ||
const inactive = normalization === "deactivated"; | ||
return { | ||
isRelevant:(u) => !inactive && isAbap(u), | ||
normalize:async (source, uri) => { | ||
if (inactive || !isAbap(uri) || (inverted === shouldNormalize(uri))) { | ||
return source; | ||
} | ||
const path = extractname(uri); | ||
try { | ||
const formatted:string = await client.sendRequest("abaplint/normalize", {path, source}); | ||
return formatted; | ||
} catch (error) { | ||
return source; | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
// registers a code formatter for bitbucket using an API which will probably never be merged | ||
// for now it's available on my fork of atlascode: | ||
// https://bitbucket.org/marcellourbani/atlascode/branch/issue-%235433-Add-hook-to-pretty-print-code-to-show-in-diff-in-atlascode | ||
// allows to: | ||
// - normalize the code by default | ||
// - get bitbucket functionality (i.e. comments) to work after normalizing | ||
export const registerBitbucket = async (client: BaseLanguageClient) => { | ||
const ext = extensions.getExtension<BitBucketApi>("atlassian.atlascode"); | ||
if (!ext) { | ||
return; | ||
} | ||
if (!ext.isActive) { | ||
await ext.activate(); | ||
} | ||
if (ext.exports?.registerCodeNormalizer) { | ||
const norm = getAbapCodeNormalizer(client); | ||
integrationIsActive = (u) => u.scheme === ATLASCODEDIFF && norm.isRelevant(u); | ||
ext.exports.registerCodeNormalizer(norm); | ||
} | ||
}; |
This file contains 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,58 @@ | ||
import {commands, ExtensionContext, TabInputTextDiff, TextEditor, Uri, window, TextDocumentContentProvider, Event, workspace} from "vscode"; | ||
import {BaseLanguageClient} from "vscode-languageclient"; | ||
import {ATLASCODEDIFF, CodeNormalizer, getAbapCodeNormalizer, integrationIsActive} from "./integrations"; | ||
const ABAPGITSCHEME = "abapgit.normalized"; | ||
|
||
const originalUri = (u:Uri) => { | ||
if (u.scheme !== ABAPGITSCHEME) {return u;} | ||
const {scheme, query} = JSON.parse(u.query); | ||
return u.with({scheme, query}); | ||
}; | ||
|
||
class NormalizedProvider implements TextDocumentContentProvider { | ||
private readonly normalizer: CodeNormalizer; | ||
public constructor(client: BaseLanguageClient) { | ||
this.normalizer = getAbapCodeNormalizer(client); | ||
}; | ||
public onDidChange?: Event<Uri> | undefined; | ||
public async provideTextDocumentContent(uri: Uri): Promise<string> { | ||
const origUri = originalUri(uri); | ||
if (uri.scheme === origUri.scheme) {throw new Error("invalid URL"); }; | ||
const raw = await workspace.openTextDocument(origUri); | ||
return this.normalizer.normalize(raw.getText(), origUri); | ||
} | ||
} | ||
|
||
const shouldActivate = (e: TextEditor | undefined) => { | ||
const curtab = window.tabGroups.activeTabGroup; | ||
const uri = e?.document.uri; | ||
const isdiff = curtab.activeTab?.input instanceof TabInputTextDiff; | ||
if (!(isdiff && uri)) {return false;} | ||
const relevant = | ||
uri.path.match(/\.abap$/) || | ||
uri.scheme === ATLASCODEDIFF && uri.fragment.match(/\.abap$/); | ||
return relevant && !integrationIsActive(uri); | ||
}; | ||
|
||
const activateNormalizer = (e: TextEditor | undefined) => { | ||
commands.executeCommand("setContext", "abaplint.IsNormalizerEnabled", shouldActivate(e)); | ||
}; | ||
const toggleUrlNormalizer = (u:Uri) => { | ||
if (u.scheme === ABAPGITSCHEME) { return originalUri(u);}; | ||
const query = JSON.stringify({scheme:u.scheme, query:u.query}); | ||
return u.with({scheme:ABAPGITSCHEME, query}); | ||
}; | ||
|
||
const toggleNormalizer = () => { | ||
const curtab = window.tabGroups.activeTabGroup.activeTab; | ||
if (!(curtab?.input instanceof TabInputTextDiff)) {return;} | ||
const {original, modified} = curtab.input; | ||
return commands.executeCommand<void>("vscode.diff", toggleUrlNormalizer(original), toggleUrlNormalizer(modified), curtab.label); | ||
}; | ||
|
||
export const registerNormalizer = (context:ExtensionContext, client: BaseLanguageClient) => { | ||
const onchg = window.onDidChangeActiveTextEditor(activateNormalizer); | ||
const normalize = commands.registerCommand("abaplint.togglediffNormalize", toggleNormalizer); | ||
const provider = workspace.registerTextDocumentContentProvider(ABAPGITSCHEME, new NormalizedProvider(client)); | ||
context.subscriptions.push(onchg, normalize, provider); | ||
}; |
This file contains 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.