Commit 84fc5da9 authored by tom's avatar tom

named imports highlight

parent 2d8cd224
...@@ -136,7 +136,15 @@ const CodeEditor = ({ data }: Props) => { ...@@ -136,7 +136,15 @@ const CodeEditor = ({ data }: Props) => {
const target = event.target as HTMLSpanElement; const target = event.target as HTMLSpanElement;
const isImportLink = target.classList.contains('import-link'); const isImportLink = target.classList.contains('import-link');
if (isImportLink) { if (isImportLink) {
const path = target.innerText; const path = [
target.previousElementSibling as HTMLSpanElement,
target,
target.nextElementSibling as HTMLSpanElement,
]
.filter((element) => element?.classList.contains('import-link'))
.map((element: HTMLSpanElement) => element.innerText)
.join('');
const fullPath = getFullPathOfImportedFile(data[index].file_path, path); const fullPath = getFullPathOfImportedFile(data[index].file_path, path);
const fileIndex = data.findIndex((file) => file.file_path === fullPath); const fileIndex = data.findIndex((file) => file.file_path === fullPath);
if (fileIndex > -1) { if (fileIndex > -1) {
...@@ -165,13 +173,11 @@ const CodeEditor = ({ data }: Props) => { ...@@ -165,13 +173,11 @@ const CodeEditor = ({ data }: Props) => {
'.highlight': { '.highlight': {
backgroundColor: themeColors['custom.findMatchHighlightBackground'], backgroundColor: themeColors['custom.findMatchHighlightBackground'],
}, },
'&&.meta-pressed .import-link': { '&&.meta-pressed .import-link:hover, &&.meta-pressed .import-link:hover + .import-link': {
_hover: {
color: themeColors['custom.fileLink.hoverForeground'], color: themeColors['custom.fileLink.hoverForeground'],
textDecoration: 'underline', textDecoration: 'underline',
cursor: 'pointer', cursor: 'pointer',
}, },
},
}), [ editorWidth, themeColors ]); }), [ editorWidth, themeColors ]);
if (data.length === 1) { if (data.length === 1) {
......
import type * as monaco from 'monaco-editor/esm/vs/editor/editor.api'; import type * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
export default function addFileImportDecorations(model: monaco.editor.ITextModel) { export default function addFileImportDecorations(model: monaco.editor.ITextModel) {
const matches = model.findMatches('^import (\'|")((\\/|\\.)(\\w|\\.|\\/|-)+)(\'|")', false, true, false, null, true); const options: monaco.editor.IModelDecorationOptions = {
const decorations: Array<monaco.editor.IModelDeltaDecoration> = matches.map(({ range }) => ({ inlineClassName: 'import-link',
hoverMessage: {
value: 'Cmd/Win + click to open file',
},
};
const regularImportMatches = model.findMatches('^import (\'|")((\\/|\\.).+)(\'|")', false, true, false, null, true);
const regularImportDecorations: Array<monaco.editor.IModelDeltaDecoration> = regularImportMatches.map(({ range }) => ({
range: { range: {
...range, ...range,
startColumn: range.startColumn + 8, startColumn: range.startColumn + 8,
endColumn: range.endColumn - 1, endColumn: range.endColumn - 1,
}, },
options: { options,
inlineClassName: 'import-link', }));
hoverMessage: {
value: 'Cmd/Win + click to open file', const namedImportMatches = model.findMatches('(^import \\{.+\\} from )(\'|")((\\/|\\.).+)(\'|")', false, true, false, null, true);
}, const namedImportDecorations: Array<monaco.editor.IModelDeltaDecoration> = namedImportMatches.map(({ range, matches }) => ({
range: {
...range,
startColumn: range.startColumn + (Array.isArray(matches) ? matches?.[1]?.length + 1 : 0),
endColumn: range.endColumn - 1,
}, },
options,
})); }));
model.deltaDecorations([], decorations);
model.deltaDecorations([], regularImportDecorations.concat(namedImportDecorations));
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment