forked from eslint/code-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
esquery
selector textfield & highlighting of matched code
Fixes eslint#79
- Loading branch information
1 parent
89a913c
commit 1102a4b
Showing
16 changed files
with
192 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
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
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,21 @@ | ||
import { useId, type FC } from "react"; | ||
import { Label } from "@/components/ui/label"; | ||
import { TextField } from "@/components/ui/text-field"; | ||
import { useExplorer } from "@/hooks/use-explorer"; | ||
|
||
export const EsquerySelectorInput: FC = () => { | ||
const { esquerySelector, setEsquerySelector } = useExplorer(); | ||
const htmlId = useId(); | ||
|
||
return ( | ||
<div className="space-y-1.5 m-2"> | ||
<Label htmlFor={htmlId}>esquery Selector</Label> | ||
<TextField | ||
id={htmlId} | ||
className="w-full" | ||
value={esquerySelector} | ||
onChange={e => setEsquerySelector(e.target.value)} | ||
/> | ||
</div> | ||
); | ||
}; |
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,27 @@ | ||
import * as React from "react"; | ||
|
||
import { cn } from "@/lib/utils"; | ||
|
||
export type TextFieldProps = Exclude< | ||
React.InputHTMLAttributes<HTMLInputElement>, | ||
"type" | ||
>; | ||
|
||
const TextField = React.forwardRef<HTMLInputElement, TextFieldProps>( | ||
({ className, ...props }, ref) => { | ||
return ( | ||
<input | ||
type="text" | ||
className={cn( | ||
"flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 border border-input bg-background hover:bg-accent hover:text-accent-foreground h-10 px-4 py-2", | ||
className, | ||
)} | ||
ref={ref} | ||
{...props} | ||
/> | ||
); | ||
}, | ||
); | ||
TextField.displayName = "TextField"; | ||
|
||
export { TextField }; |
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,39 @@ | ||
import esquery from "esquery"; | ||
import { Node } from "acorn"; | ||
import type { Node as EstreeNode } from "estree"; | ||
import { useExplorer } from "@/hooks/use-explorer"; | ||
import { parseJavascriptAST } from "@/lib/parse-javascript-ast"; | ||
import type { HighlightRange } from "@/utils/highlight-ranges"; | ||
|
||
export function useHighlightRanges() { | ||
const { language, code, jsOptions, esquerySelector } = useExplorer(); | ||
|
||
let highlightRanges: HighlightRange[] = []; | ||
if (language === "javascript" && jsOptions.esquerySelectorEnabled) { | ||
if (jsOptions.esquerySelectorEnabled) { | ||
try { | ||
const tree = parseJavascriptAST({ | ||
code: code.javascript, | ||
jsOptions: jsOptions, | ||
}); | ||
/** | ||
* "esquery" uses type "Node" of "@types/estree", "espree" returns "Node" of "acorn" | ||
* but they are compatible | ||
* Therefore, cast between "Node" of "acorn" and "Node" of "@types/estree" | ||
*/ | ||
const esqueryMatchedNodes = esquery.match( | ||
tree as EstreeNode, | ||
esquery.parse(esquerySelector), | ||
) as Node[]; | ||
highlightRanges = esqueryMatchedNodes.map(node => [ | ||
node.start, | ||
node.end, | ||
]); | ||
} catch { | ||
// ignore | ||
} | ||
} | ||
} | ||
|
||
return highlightRanges; | ||
} |
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,15 @@ | ||
import type { JsOptions } from "@/hooks/use-explorer"; | ||
import * as espree from "espree"; | ||
|
||
export function parseJavascriptAST(opts: { | ||
code: string; | ||
jsOptions: JsOptions; | ||
}) { | ||
return espree.parse(opts.code, { | ||
ecmaVersion: opts.jsOptions.esVersion, | ||
sourceType: opts.jsOptions.sourceType, | ||
ecmaFeatures: { | ||
jsx: opts.jsOptions.isJSX, | ||
}, | ||
}); | ||
} |
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,16 @@ | ||
import { Decoration, ViewPlugin } from "@codemirror/view"; | ||
|
||
const highlightRangeDecoration = Decoration.mark({ | ||
class: "eslint-code-explorer_highlight-range", | ||
}); | ||
export type HighlightRange = [rangeFrom: number, rangeTo: number]; | ||
|
||
export const highlightRangesExtension = (ranges: HighlightRange[]) => | ||
ViewPlugin.define(() => ({}), { | ||
decorations: () => | ||
Decoration.set( | ||
ranges.map(([rangeFrom, rangeTo]) => | ||
highlightRangeDecoration.range(rangeFrom, rangeTo), | ||
), | ||
), | ||
}); |
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