Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: linghaoSu/vscode-regex-visualizer
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.0.5
Choose a base ref
...
head repository: linghaoSu/vscode-regex-visualizer
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
  • 6 commits
  • 6 files changed
  • 1 contributor

Commits on May 9, 2023

  1. Copy the full SHA
    bfa0eb8 View commit details
  2. Copy the full SHA
    8f4bdc7 View commit details
  3. Merge pull request #7 from linghaoSu/fix/flags

    Fix/flags
    linghaoSu authored May 9, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    84fe81f View commit details
  4. release v0.0.6

    linghaoSu committed May 9, 2023
    Copy the full SHA
    4b07653 View commit details
  5. Update README.md

    linghaoSu authored May 9, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    7a42683 View commit details
  6. Update README.md

    linghaoSu authored May 9, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    2fb9f95 View commit details
Showing with 116 additions and 97 deletions.
  1. +9 −1 README.md
  2. +1 −1 package.json
  3. +86 −0 src/constant.ts
  4. +4 −88 src/index.ts
  5. +3 −1 src/log.ts
  6. +13 −6 src/utils.ts
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -7,6 +7,14 @@

Aim to visualize regex to easy understand complex regex

The regular visualization method comes from this [project](https://gitlab.com/javallone/regexper-static)

The regularity used to detect regular expressions comes from this [project](https://github.com/chrmarti/vscode-regex/blob/41062efe8aa5113e8902742ae270e090a3de5c5e/src/extension.ts#L14)

Hover over a regular expression to display:

<img width="763" alt="image" src="https://github.com/linghaoSu/vscode-regex-visualizer/assets/56526981/614e9505-a40a-42f9-952c-a1fadd9c8a06">

## License

[MIT](./LICENSE) License © 2022
[MIT](./LICENSE) License © 2023
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
},
"name": "regex-visualizer",
"displayName": "Regex Visualizer",
"version": "0.0.5",
"version": "0.0.6",
"packageManager": "pnpm@7.0.1",
"description": "",
"license": "MIT",
86 changes: 86 additions & 0 deletions src/constant.ts
Original file line number Diff line number Diff line change
@@ -5,3 +5,89 @@ export const mockWindow = new Window();
export const mockDocument = mockWindow.document;

export const getCommandName = (name: string) => `${EXT_NAME}.${name}`;

export const baseHTMLContent = `
<div id="regexp-render"><svg></svg></div>
<script type="text/html" id="svg-container-base">
<div class="svg">
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
version="1.1">
<defs>
<style type="text/css">svg {
background-color: #fff; }
.root text,
.root tspan {
font: 12px Arial; }
.root path {
fill-opacity: 0;
stroke-width: 2px;
stroke: #000; }
.root circle {
fill: #6b6659;
stroke-width: 2px;
stroke: #000; }
.anchor text, .any-character text {
fill: #fff; }
.anchor rect, .any-character rect {
fill: #6b6659; }
.escape text, .charset-escape text, .literal text {
fill: #000; }
.escape rect, .charset-escape rect {
fill: #bada55; }
.literal rect {
fill: #dae9e5; }
.charset .charset-box {
fill: #cbcbba; }
.subexp .subexp-label tspan,
.charset .charset-label tspan,
.match-fragment .repeat-label tspan {
font-size: 10px; }
.repeat-label {
cursor: help; }
.subexp .subexp-label tspan,
.charset .charset-label tspan {
dominant-baseline: text-after-edge; }
.subexp .subexp-box {
stroke: #908c83;
stroke-dasharray: 6,2;
stroke-width: 2px;
fill-opacity: 0; }
.quote {
fill: #908c83; }
</style>
</defs>
<metadata>
<rdf:RDF>
<cc:License rdf:about="http://creativecommons.org/licenses/by/3.0/">
<cc:permits rdf:resource="http://creativecommons.org/ns#Reproduction" />
<cc:permits rdf:resource="http://creativecommons.org/ns#Distribution" />
<cc:requires rdf:resource="http://creativecommons.org/ns#Notice" />
<cc:requires rdf:resource="http://creativecommons.org/ns#Attribution" />
<cc:permits rdf:resource="http://creativecommons.org/ns#DerivativeWorks" />
</cc:License>
</rdf:RDF>
</metadata>
</svg>
</div>
<div class="progress">
<div style="width:0;"></div>
</div>
</script>
`;
92 changes: 4 additions & 88 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -2,97 +2,13 @@ import type { ExtensionContext, TextDocument } from 'vscode';
import { MarkdownString, OverviewRulerLane, Range, window, workspace } from 'vscode';
import { disposeSettingListener, initialSetting } from './settings';
import { Log } from './log';
import { EXT_NAME, mockDocument } from './constant';
import { EXT_NAME, baseHTMLContent, mockDocument } from './constant';
import { svg64 } from './svg64';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
import Regexper from './regexp-visualizer/js/regexper.js';

mockDocument.body.innerHTML = `
<div id="regexp-render"><svg></svg></div>
<script type="text/html" id="svg-container-base">
<div class="svg">
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
version="1.1">
<defs>
<style type="text/css">svg {
background-color: #fff; }
.root text,
.root tspan {
font: 12px Arial; }
.root path {
fill-opacity: 0;
stroke-width: 2px;
stroke: #000; }
.root circle {
fill: #6b6659;
stroke-width: 2px;
stroke: #000; }
.anchor text, .any-character text {
fill: #fff; }
.anchor rect, .any-character rect {
fill: #6b6659; }
.escape text, .charset-escape text, .literal text {
fill: #000; }
.escape rect, .charset-escape rect {
fill: #bada55; }
.literal rect {
fill: #dae9e5; }
.charset .charset-box {
fill: #cbcbba; }
.subexp .subexp-label tspan,
.charset .charset-label tspan,
.match-fragment .repeat-label tspan {
font-size: 10px; }
.repeat-label {
cursor: help; }
.subexp .subexp-label tspan,
.charset .charset-label tspan {
dominant-baseline: text-after-edge; }
.subexp .subexp-box {
stroke: #908c83;
stroke-dasharray: 6,2;
stroke-width: 2px;
fill-opacity: 0; }
.quote {
fill: #908c83; }
</style>
</defs>
<metadata>
<rdf:RDF>
<cc:License rdf:about="http://creativecommons.org/licenses/by/3.0/">
<cc:permits rdf:resource="http://creativecommons.org/ns#Reproduction" />
<cc:permits rdf:resource="http://creativecommons.org/ns#Distribution" />
<cc:requires rdf:resource="http://creativecommons.org/ns#Notice" />
<cc:requires rdf:resource="http://creativecommons.org/ns#Attribution" />
<cc:permits rdf:resource="http://creativecommons.org/ns#DerivativeWorks" />
</cc:License>
</rdf:RDF>
</metadata>
</svg>
</div>
<div class="progress">
<div style="width:0;"></div>
</div>
</script>
`;
mockDocument.body.innerHTML = baseHTMLContent;
interface RegexMatch {
document: TextDocument
regex: RegExp
@@ -147,7 +63,7 @@ export function activate(context: ExtensionContext) {

const regexper = new Regexper(mockDocument.body);

await regexper.showExpression(regexStr.slice(1, -1));
await regexper.showExpression(regexStr);

const svgParentContainer = regexper.svgContainer.querySelector('svg');

@@ -158,7 +74,7 @@ export function activate(context: ExtensionContext) {

const result = base64SVGStr;

const dom = `<img src="${result}" />`;
const dom = `<img src="${result}" /><br/>[Open in the browser](https://regexper.com/#${encodeURIComponent(regexStr)})`;
const message = new MarkdownString(dom);

message.isTrusted = true;
4 changes: 3 additions & 1 deletion src/log.ts
Original file line number Diff line number Diff line change
@@ -45,7 +45,9 @@ export class Log {
: `${EXT_NAME} Error: ${err.toString()}`;

const result = await window.showErrorMessage(message, openOutputButton);
if (result === openOutputButton) { this.show(); }
if (result === openOutputButton) {
this.show();
}
}
}

19 changes: 13 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -27,31 +27,38 @@ export function getWorkspaceInfo() {
info.schemaPath = `${folder.uri.toString()}${sep}`;
info.uri = folder.uri;
}
}
else {
} else {
return false;
}
return info;
}

export function isTargetFile(path: string) {
// only check vue file for now
if (/.*\.vue$/.test(path)) { return true; }
if (/.*\.vue$/.test(path)) {
return true;
}
return false;
}

export function getRelativePath(path: string) {
const info = getWorkspaceInfo();
if (!info) { return false; }
if (!info) {
return false;
}

if (path.startsWith(info.schemaPath)) { return path.slice((info.schemaPath).length); }
if (path.startsWith(info.schemaPath)) {
return path.slice((info.schemaPath).length);
}

return path;
}

export function getAbsoluteUri(path: string[]) {
const info = getWorkspaceInfo();
if (!info || !info.uri) { return false; }
if (!info || !info.uri) {
return false;
}

return Uri.joinPath(info.uri, ...path);
}