Skip to content

Commit a22d2ce

Browse files
committed
Enhance PHP syntax highlighting and theme consistency across editor components
1 parent f8758d6 commit a22d2ce

6 files changed

Lines changed: 118 additions & 17 deletions

File tree

‎docs/.vitepress/theme/components/PlaygroundEditor.vue‎

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ const baseTheme = EditorView.theme({
105105
'.cm-php-self-static, .cm-php-self-static *': { color: '#e5c07b !important', fontStyle: 'italic !important', fontWeight: '600 !important' },
106106
'.cm-php-property, .cm-php-property *': { color: '#cbd3dd !important', fontWeight: '500 !important' },
107107
'.cm-phpdoc-primitive, .cm-phpdoc-primitive *': { color: '#56b6c2 !important', fontWeight: '600 !important', fontStyle: 'normal !important' },
108+
'.cm-php-keyword, .cm-php-keyword *': { color: '#c678dd !important', fontWeight: '700 !important' },
108109
});
109110
110111
onMounted(() => {
@@ -320,7 +321,7 @@ defineExpose({
320321
height: 100%;
321322
position: relative;
322323
overflow: hidden;
323-
background-color: var(--vp-c-bg);
324+
background-color: var(--tp-editor-bg, var(--vp-c-bg));
324325
}
325326
326327
.playground-editor-wrapper :deep(.cm-editor) {
@@ -471,10 +472,22 @@ defineExpose({
471472
472473
:root:not(.dark) .playground-editor-wrapper :deep(.cm-php-property),
473474
:root:not(.dark) .playground-editor-wrapper :deep(.cm-php-property) * {
474-
color: #0184bc !important;
475+
color: #1d2023 !important;
475476
font-weight: 500 !important;
476477
}
477478
479+
:root:not(.dark) .playground-editor-wrapper :deep(.cm-php-keyword),
480+
:root:not(.dark) .playground-editor-wrapper :deep(.cm-php-keyword) * {
481+
color: #a626a4 !important;
482+
font-weight: 700 !important;
483+
}
484+
485+
:root:not(.dark) .playground-editor-wrapper :deep(.cm-phpdoc-primitive),
486+
:root:not(.dark) .playground-editor-wrapper :deep(.cm-phpdoc-primitive) * {
487+
color: #0184bc !important;
488+
font-weight: 600 !important;
489+
}
490+
478491
@media (max-width: 768px) {
479492
.xray-floating-badge {
480493
display: none !important;
@@ -726,14 +739,47 @@ defineExpose({
726739
text-underline-offset: 2px;
727740
}
728741
742+
.playground-editor-wrapper :deep(.cm-gutters) {
743+
padding-right: 0;
744+
}
745+
746+
.playground-editor-wrapper :deep(.cm-lineNumbers .cm-gutterElement) {
747+
min-width: 22px;
748+
padding: 0 3px 0 6px;
749+
}
750+
751+
.playground-editor-wrapper :deep(.cm-foldGutter) {
752+
width: 11px;
753+
}
754+
755+
.playground-editor-wrapper :deep(.cm-foldGutter .cm-gutterElement) {
756+
padding: 0 2px 0 0;
757+
font-size: 11px;
758+
text-align: center;
759+
}
760+
729761
.playground-editor-wrapper :deep(.cm-gutter-lint) {
730-
width: 14px;
762+
width: 0;
763+
overflow: hidden;
764+
transition: width 0.12s ease;
765+
}
766+
767+
.playground-editor-wrapper :deep(.cm-gutter-lint:has(.cm-lint-marker)) {
768+
width: 12px;
769+
overflow: visible;
770+
}
771+
772+
.playground-editor-wrapper :deep(.cm-gutter-lint .cm-gutterElement) {
773+
padding: 0;
774+
display: flex;
775+
align-items: center;
776+
justify-content: center;
731777
}
732778
733779
.playground-editor-wrapper :deep(.cm-lint-marker) {
734-
width: 8px;
735-
height: 8px;
736-
margin-top: 5px;
780+
width: 7px;
781+
height: 7px;
782+
margin: 0;
737783
}
738784
739785
.playground-editor-wrapper :deep(.cm-lint-marker-error) {

‎docs/.vitepress/theme/components/TypePHPSnippet.vue‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,7 @@ function copyCode() {
960960
width: 100%;
961961
position: relative;
962962
overflow: hidden;
963+
--tp-editor-bg: var(--vp-code-block-bg, var(--vp-c-bg-soft));
963964
}
964965
965966
.code-view-pane {
@@ -1046,6 +1047,12 @@ function copyCode() {
10461047
display: block;
10471048
}
10481049
1050+
.snippet-pre,
1051+
.snippet-pre code {
1052+
font-variant-ligatures: none;
1053+
font-feature-settings: "liga" 0, "calt" 0;
1054+
}
1055+
10491056
.snippet-pre {
10501057
margin: 0 !important;
10511058
padding: 18px 20px 18px 52px !important;

‎docs/.vitepress/theme/components/scripts/phpdoc-highlighter.ts‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const funcDeco = Decoration.mark({ class: 'cm-php-func' });
1818
const thisDeco = Decoration.mark({ class: 'cm-php-this' });
1919
const selfStaticDeco = Decoration.mark({ class: 'cm-php-self-static' });
2020
const propertyDeco = Decoration.mark({ class: 'cm-php-property' });
21+
const keywordDeco = Decoration.mark({ class: 'cm-php-keyword' });
2122

2223
interface LineToken {
2324
from: number;
@@ -45,7 +46,8 @@ const PHP_KEYWORDS = new Set([
4546
'continue', 'return', 'throw', 'try', 'catch', 'finally', 'declare', 'function', 'fn',
4647
'class', 'interface', 'trait', 'enum', 'extends', 'implements', 'use', 'new', 'clone',
4748
'echo', 'print', 'include', 'include_once', 'require', 'require_once', 'isset', 'empty',
48-
'unset', 'eval', 'exit', 'die', 'match', 'yield', 'array', 'list'
49+
'unset', 'eval', 'exit', 'die', 'match', 'yield', 'array', 'list',
50+
'public', 'protected', 'private', 'readonly',
4951
]);
5052

5153
function getIgnoredCodeRanges(text: string): Array<{ from: number; to: number }> {
@@ -298,6 +300,54 @@ function tokenizePhpCode(lineFrom: number, text: string, lineTokens: LineToken[]
298300
}
299301
}
300302

303+
const classConstKeywordRegex = /::\s*(class)\b/g;
304+
while ((match = classConstKeywordRegex.exec(text)) !== null) {
305+
const from = lineFrom + match.index + match[0].indexOf('class');
306+
const to = from + 5;
307+
if (!isInsideIgnored(from - lineFrom, to - lineFrom, ignored)) {
308+
lineTokens.push({ from, to, deco: keywordDeco });
309+
}
310+
}
311+
312+
const readonlyRegex =
313+
/\b(readonly)\b(?=\s+(?:class|final|abstract|public|protected|private|static|var|[?\\a-zA-Z_$]))/g;
314+
while ((match = readonlyRegex.exec(text)) !== null) {
315+
const startPos = match.index;
316+
if (isInsideIgnored(startPos, startPos + 8, ignored)) continue;
317+
const prevChar = startPos > 0 ? text[startPos - 1] : '';
318+
const twoBefore = startPos > 1 ? text.slice(startPos - 2, startPos) : '';
319+
const beforeLine = text.slice(0, startPos);
320+
if (
321+
prevChar === '$' ||
322+
twoBefore === '->' ||
323+
twoBefore === '::' ||
324+
/\bfunction\s+&?$/.test(beforeLine)
325+
) {
326+
continue;
327+
}
328+
lineTokens.push({ from: lineFrom + startPos, to: lineFrom + startPos + 8, deco: keywordDeco });
329+
}
330+
331+
const enumDeclMatch = /^\s*(enum)\s+[A-Za-z_]/.exec(text);
332+
if (enumDeclMatch) {
333+
const startPos = text.indexOf('enum');
334+
if (!isInsideIgnored(startPos, startPos + 4, ignored)) {
335+
lineTokens.push({ from: lineFrom + startPos, to: lineFrom + startPos + 4, deco: keywordDeco });
336+
}
337+
}
338+
339+
const asymVisRegex = /\b(public|protected|private)\s*\(\s*(set)\s*\)/g;
340+
while ((match = asymVisRegex.exec(text)) !== null) {
341+
const visStart = match.index;
342+
const visEnd = visStart + match[1].length;
343+
const setStart = match.index + match[0].lastIndexOf('set');
344+
const setEnd = setStart + 3;
345+
if (!isInsideIgnored(visStart, setEnd, ignored)) {
346+
lineTokens.push({ from: lineFrom + visStart, to: lineFrom + visEnd, deco: keywordDeco });
347+
lineTokens.push({ from: lineFrom + setStart, to: lineFrom + setEnd, deco: keywordDeco });
348+
}
349+
}
350+
301351
const propertyAccessRegex = /(?:->|\?->)\s*([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\b(?!\s*(\(|\.\.\.))/g;
302352
while ((match = propertyAccessRegex.exec(text)) !== null) {
303353
const propName = match[1];

‎docs/.vitepress/theme/components/scripts/theme-one-dark.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { tags as t } from '@lezer/highlight';
55
export const oneDarkTheme = EditorView.theme({
66
'&': {
77
color: '#cbd3dd',
8-
backgroundColor: 'var(--vp-c-bg, #1e1e20)',
8+
backgroundColor: 'var(--tp-editor-bg, var(--vp-c-bg, #1e1e20))',
99
},
1010
'.cm-content': {
1111
caretColor: '#528bff',
@@ -20,7 +20,7 @@ export const oneDarkTheme = EditorView.theme({
2020
backgroundColor: 'rgba(255, 255, 255, 0.04)',
2121
},
2222
'.cm-gutters': {
23-
backgroundColor: 'var(--vp-c-bg, #1e1e20)',
23+
backgroundColor: 'var(--tp-editor-bg, var(--vp-c-bg, #1e1e20))',
2424
color: '#5c6370',
2525
borderRight: '1px solid var(--vp-c-divider, #2e2e32)',
2626
},
@@ -31,12 +31,12 @@ export const oneDarkTheme = EditorView.theme({
3131
}, { dark: true });
3232

3333
export const oneDarkHighlightStyle = HighlightStyle.define([
34-
{ tag: [t.keyword, t.modifier], color: '#c678dd', fontWeight: 'bold' },
34+
{ tag: [t.keyword, t.modifier, t.processingInstruction], color: '#c678dd', fontWeight: 'bold' },
3535
{ tag: [t.variableName, t.definition(t.variableName)], color: '#cbd3dd' },
3636
{ tag: [t.propertyName], color: '#cbd3dd' },
3737
{ tag: [t.function(t.variableName), t.function(t.propertyName)], color: '#61afef' },
3838
{ tag: [t.typeName, t.className], color: '#e5c07b', fontWeight: '600' },
39-
{ tag: [t.string, t.special(t.string)], color: '#98c379' },
39+
{ tag: [t.string, t.special(t.string), t.escape], color: '#98c379' },
4040
{ tag: [t.number], color: '#d19a66' },
4141
{ tag: [t.comment, t.lineComment, t.blockComment], color: '#5c6370', fontStyle: 'italic' },
4242
{ tag: [t.operator, t.punctuation], color: '#abb2bf' },

‎docs/.vitepress/theme/components/scripts/theme-one-light.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { tags as t } from '@lezer/highlight';
55
export const oneLightTheme = EditorView.theme({
66
'&': {
77
color: '#1d2023',
8-
backgroundColor: 'var(--vp-c-bg, #ffffff)',
8+
backgroundColor: 'var(--tp-editor-bg, var(--vp-c-bg, #ffffff))',
99
},
1010
'.cm-content': {
1111
caretColor: '#528bff',
@@ -20,7 +20,7 @@ export const oneLightTheme = EditorView.theme({
2020
backgroundColor: 'rgba(0, 0, 0, 0.035)',
2121
},
2222
'.cm-gutters': {
23-
backgroundColor: 'var(--vp-c-bg, #ffffff)',
23+
backgroundColor: 'var(--tp-editor-bg, var(--vp-c-bg, #ffffff))',
2424
color: '#9d9d9f',
2525
borderRight: '1px solid var(--vp-c-divider, #e2e2e3)',
2626
},
@@ -31,12 +31,12 @@ export const oneLightTheme = EditorView.theme({
3131
}, { dark: false });
3232

3333
export const oneLightHighlightStyle = HighlightStyle.define([
34-
{ tag: [t.keyword, t.modifier], color: '#a626a4', fontWeight: 'bold' },
34+
{ tag: [t.keyword, t.modifier, t.processingInstruction], color: '#a626a4', fontWeight: 'bold' },
3535
{ tag: [t.variableName, t.definition(t.variableName)], color: '#1d2023' },
3636
{ tag: [t.propertyName], color: '#1d2023' },
3737
{ tag: [t.function(t.variableName), t.function(t.propertyName)], color: '#4078f2' },
3838
{ tag: [t.typeName, t.className], color: '#c18401', fontWeight: '600' },
39-
{ tag: [t.string, t.special(t.string)], color: '#50a14f' },
39+
{ tag: [t.string, t.special(t.string), t.escape], color: '#22863a' },
4040
{ tag: [t.number], color: '#986801' },
4141
{ tag: [t.comment, t.lineComment, t.blockComment], color: '#a0a1a7', fontStyle: 'italic' },
4242
{ tag: [t.operator, t.punctuation], color: '#383a42' },

‎docs/.vitepress/theme/styles/docblocks.css‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ html.dark .cm-php-this {
8888
}
8989

9090
html.dark .cm-php-self-static { color: #e5c07b !important; font-style: italic !important; font-weight: 600 !important; }
91-
html.dark .cm-php-property { color: #56b6c2 !important; font-weight: 500 !important; }
9291

9392
html:not(.dark) .tok-phpdoc-tag,
9493
html:not(.dark) .cm-phpdoc-tag {
@@ -180,7 +179,6 @@ html:not(.dark) .cm-php-this {
180179
}
181180

182181
html:not(.dark) .cm-php-self-static { color: #c18401 !important; font-style: italic !important; font-weight: 600 !important; }
183-
html:not(.dark) .cm-php-property { color: #0184bc !important; font-weight: 500 !important; }
184182

185183
html.dark .tok-phpdoc-primitive,
186184
html.dark .cm-phpdoc-primitive {

0 commit comments

Comments
 (0)