-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from code-hike/regex-range
Regex range
- Loading branch information
Showing
15 changed files
with
4,949 additions
and
757 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@code-hike/lighter": patch | ||
--- | ||
|
||
Add regex annotations |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,18 @@ | ||
import { test, expect } from "vitest"; | ||
import { extractor } from "./extractor"; | ||
|
||
test("extraction", () => { | ||
expectExtraction("!foo", ["foo", , ""]); | ||
expectExtraction("!foo(1)", ["foo", "(1)", ""]); | ||
expectExtraction("!foo[1]", ["foo", "[1]", ""]); | ||
expectExtraction("!foo[ ] q q", ["foo", "[ ]", "q q"]); | ||
expectExtraction("!foo(/bar/)", ["foo", "(/bar/)", ""]); | ||
expectExtraction("!foo(/bar baz/g) 1 2", ["foo", "(/bar baz/g)", "1 2"]); | ||
expectExtraction("!Focus(/y/) bar", ["Focus", "(/y/)", "bar"]); | ||
}); | ||
|
||
function expectExtraction(comment: string, expected: [string, string, string]) { | ||
const result = extractor(comment); | ||
const [name, rangeString, query] = expected; | ||
expect(result).toEqual({ name, rangeString, query }); | ||
} |
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,19 @@ | ||
export function extractor(comment: string, prefix: string = "!") { | ||
// const regex = /\s*(!?[\w-]+)?(\([^\)]*\)|\[[^\]]*\])?(.*)$/; | ||
|
||
const regex = new RegExp( | ||
`\\s*(${prefix}?[\\w-]+)?(\\([^\\)]*\\)|\\[[^\\]]*\\])?(.*)$` | ||
); | ||
const match = comment.match(regex); | ||
const name = match[1]; | ||
const rangeString = match[2]; | ||
const query = match[3]?.trim(); | ||
if (!name || !name.startsWith(prefix)) { | ||
return null; | ||
} | ||
return { | ||
name: name.slice(prefix.length), | ||
rangeString, | ||
query, | ||
}; | ||
} |
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,123 @@ | ||
import { expect, test } from "vitest"; | ||
import { blockRegexToRange, inlineRegexToRange } from "./regex-range"; | ||
|
||
test("block regex range", () => { | ||
const code = ` | ||
function x() { | ||
console.log(1) | ||
console.log(2) | ||
if (true) { | ||
console.log(3) | ||
} | ||
} | ||
function y() { | ||
console.log(1) | ||
} | ||
`.trim(); | ||
const result = blockRegexToRange(code, "(/^ {2}.*(?:\n {2}.*)*/gm)", 1); | ||
expect(result).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"fromLineNumber": 2, | ||
"toLineNumber": 6, | ||
}, | ||
{ | ||
"fromLineNumber": 10, | ||
"toLineNumber": 10, | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
test("block regex range repeated line", () => { | ||
const code = ` | ||
const foo = 1; | ||
const bar = 2 | ||
foo = foo + bar; | ||
`.trim(); | ||
const result = blockRegexToRange(code, "(/foo/g)", 1); | ||
expect(result).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"fromLineNumber": 1, | ||
"toLineNumber": 1, | ||
}, | ||
{ | ||
"fromLineNumber": 3, | ||
"toLineNumber": 3, | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
test("inline regex range", () => { | ||
const code = ` | ||
const foo = 1; | ||
const bar = 2 | ||
foo = foo + bar; | ||
`.trim(); | ||
const result = inlineRegexToRange(code, "[/foo/g]", 1); | ||
|
||
expect(result).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"fromColumn": 7, | ||
"lineNumber": 1, | ||
"toColumn": 9, | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
test("inline regex range with capture group", () => { | ||
const code = ` | ||
function C() { | ||
return <div className="bg-red-500"> | ||
<span className="text-blue-500"><a className="x">Hello</a></span> | ||
</div> | ||
} | ||
`.trim(); | ||
const result = inlineRegexToRange(code, '[/className="(.*?)"/gm]', 1); | ||
|
||
expect(result).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"fromColumn": 26, | ||
"lineNumber": 2, | ||
"toColumn": 35, | ||
}, | ||
{ | ||
"fromColumn": 22, | ||
"lineNumber": 3, | ||
"toColumn": 34, | ||
}, | ||
{ | ||
"fromColumn": 51, | ||
"lineNumber": 3, | ||
"toColumn": 51, | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
test("inline regex range with capture group not g", () => { | ||
const code = ` | ||
function C() { | ||
return <div className=""> | ||
<span className="text-blue-500"><a className="x">Hello</a></span> | ||
</div> | ||
} | ||
`.trim(); | ||
const result = inlineRegexToRange(code, '[/className="(.*?)"/]', 3); | ||
|
||
expect(result).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"fromColumn": 22, | ||
"lineNumber": 3, | ||
"toColumn": 34, | ||
}, | ||
] | ||
`); | ||
}); |
Oops, something went wrong.