Skip to content

Commit

Permalink
Don't require '<?php'
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Jan 16, 2024
1 parent bdc327e commit 90a1a08
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {useEffect, useRef} from "react";
import {editor} from "monaco-editor";
import ITextModel = editor.ITextModel;
import { useHotkeys } from "react-hotkeys-hook";
import { registerLanguage } from "./registerLanguage.ts";
//import {Command} from "@tauri-apps/api/shell";

export default function Editor(props: {
Expand Down Expand Up @@ -58,17 +59,21 @@ export default function Editor(props: {
monaco.editor.onDidCreateModel(modelCallback),
];

registerLanguage(monaco.languages)
.forEach(d => disposables.push(d));

const models = monaco.editor.getModels();
if (models.length) {
modelCallback(models[0]);
}

return () => disposables.forEach(d => d.dispose());
}, [monaco]);

return (
<Monaco
height="100vh"
defaultLanguage="php"
defaultLanguage="php-snippet"
theme="vs-dark"
defaultValue={defaultValue}
/>
Expand Down
245 changes: 245 additions & 0 deletions src/registerLanguage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
import { languages as MonacoLanguages } from "monaco-editor";
import { IDisposable } from "./disposables.ts";

// See: https://github.com/microsoft/monaco-editor/blob/main/src/basic-languages/php/php.ts

const conf: MonacoLanguages.LanguageConfiguration = {
wordPattern:
/(-?\d*\.\d\w*)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g,

comments: {
lineComment: '//',
blockComment: ['/*', '*/']
},

brackets: [
['{', '}'],
['[', ']'],
['(', ')']
],

autoClosingPairs: [
{ open: '{', close: '}', notIn: ['string'] },
{ open: '[', close: ']', notIn: ['string'] },
{ open: '(', close: ')', notIn: ['string'] },
{ open: '"', close: '"', notIn: ['string'] },
{ open: "'", close: "'", notIn: ['string', 'comment'] }
],

folding: {
markers: {
start: new RegExp('^\\s*(#|//)region\\b'),
end: new RegExp('^\\s*(#|//)endregion\\b')
}
}
};

const language = <MonacoLanguages.IMonarchLanguage>{
defaultToken: '',
tokenPostfix: '',
// ignoreCase: true,

// The main tokenizer for our languages
tokenizer: {

root: [
[
/[a-zA-Z_]\w*/,
{
cases: {
'@phpKeywords': { token: 'keyword.php' },
'@phpCompileTimeConstants': { token: 'constant.php' },
'@default': 'identifier.php'
}
}
],
[
/[$a-zA-Z_]\w*/,
{
cases: {
'@phpPreDefinedVariables': {
token: 'variable.predefined.php'
},
'@default': 'variable.php'
}
}
],

// brackets
[/[{}]/, 'delimiter.bracket.php'],
[/[\[\]]/, 'delimiter.array.php'],
[/[()]/, 'delimiter.parenthesis.php'],

// whitespace
[/[ \t\r\n]+/],

// comments
[/(#|\/\/)$/, 'comment.php'],
[/(#|\/\/)/, 'comment.php', '@phpLineComment'],

// block comments
[/\/\*/, 'comment.php', '@phpComment'],

// strings
[/"/, 'string.php', '@phpDoubleQuoteString'],
[/'/, 'string.php', '@phpSingleQuoteString'],

// delimiters
[/[\+\-\*\%\&\|\^\~\!\=\<\>\/\?\;\:\.\,\@]/, 'delimiter.php'],

// numbers
[/\d*\d+[eE]([\-+]?\d+)?/, 'number.float.php'],
[/\d*\.\d+([eE][\-+]?\d+)?/, 'number.float.php'],
[/0[xX][0-9a-fA-F']*[0-9a-fA-F]/, 'number.hex.php'],
[/0[0-7']*[0-7]/, 'number.octal.php'],
[/0[bB][0-1']*[0-1]/, 'number.binary.php'],
[/\d[\d']*/, 'number.php'],
[/\d/, 'number.php']
],

phpComment: [
[/\*\//, 'comment.php', '@pop'],
[/[^*]+/, 'comment.php'],
[/./, 'comment.php']
],

phpLineComment: [
[/\?>/, { token: '@rematch', next: '@pop' }],
[/.$/, 'comment.php', '@pop'],
[/[^?]+$/, 'comment.php', '@pop'],
[/[^?]+/, 'comment.php'],
[/./, 'comment.php']
],

phpDoubleQuoteString: [
[/[^\\"]+/, 'string.php'],
[/@escapes/, 'string.escape.php'],
[/\\./, 'string.escape.invalid.php'],
[/"/, 'string.php', '@pop']
],

phpSingleQuoteString: [
[/[^\\']+/, 'string.php'],
[/@escapes/, 'string.escape.php'],
[/\\./, 'string.escape.invalid.php'],
[/'/, 'string.php', '@pop']
]
},

phpKeywords: [
'abstract',
'and',
'array',
'as',
'break',
'callable',
'case',
'catch',
'cfunction',
'class',
'clone',
'const',
'continue',
'declare',
'default',
'do',
'else',
'elseif',
'enddeclare',
'endfor',
'endforeach',
'endif',
'endswitch',
'endwhile',
'extends',
'false',
'final',
'for',
'foreach',
'function',
'global',
'goto',
'if',
'implements',
'interface',
'instanceof',
'insteadof',
'namespace',
'new',
'null',
'object',
'old_function',
'or',
'private',
'protected',
'public',
'resource',
'static',
'switch',
'throw',
'trait',
'try',
'true',
'use',
'var',
'while',
'xor',
'die',
'echo',
'empty',
'exit',
'eval',
'include',
'include_once',
'isset',
'list',
'require',
'require_once',
'return',
'print',
'unset',
'yield',
'__construct'
],

phpCompileTimeConstants: [
'__CLASS__',
'__DIR__',
'__FILE__',
'__LINE__',
'__NAMESPACE__',
'__METHOD__',
'__FUNCTION__',
'__TRAIT__'
],

phpPreDefinedVariables: [
'$GLOBALS',
'$_SERVER',
'$_GET',
'$_POST',
'$_FILES',
'$_REQUEST',
'$_SESSION',
'$_ENV',
'$_COOKIE',
'$php_errormsg',
'$HTTP_RAW_POST_DATA',
'$http_response_header',
'$argc',
'$argv'
],

escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/
};

export const registerLanguage = (languages: typeof MonacoLanguages): IDisposable[] => {
const languageId = 'php-snippet'

languages.register({ id: languageId })

return [
languages.setLanguageConfiguration(languageId, conf),
languages.setMonarchTokensProvider(languageId, language)
];
}
2 changes: 1 addition & 1 deletion src/useEditorValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export default function useEditorValue(): [string, (value: string) => void] {
function valueRetriever() {
const value = localStorage.getItem('value');

return value ? value : '';
return value ? value.replace(/^\s*<\?(php)?\s*/i, '') : '';
}

0 comments on commit 90a1a08

Please sign in to comment.