├── .gitignore ├── .npmignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── package.json └── src ├── README.md └── php.ts /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | package-lock.json 3 | /dist 4 | /test/*.js 5 | /test/*.d.ts 6 | /test/*.d.ts.map 7 | .tern-* 8 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /src 2 | /test 3 | /node_modules 4 | .tern-* 5 | rollup.config.js 6 | tsconfig.json 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 6.0.1 (2022-10-24) 2 | 3 | ### Bug fixes 4 | 5 | Fix (non-)auto indentation in template strings and comments. 6 | 7 | Allow prefixed strings to be closed by `closeBrackets`. 8 | 9 | ## 6.0.0 (2022-06-08) 10 | 11 | ### Breaking changes 12 | 13 | Update dependencies to 6.0.0 14 | 15 | ## 0.20.0 (2022-04-20) 16 | 17 | ### Bug fixes 18 | 19 | Fix missing highlighting info for the `declare`, `enddeclare`, and `interface` keywords. 20 | 21 | ## 0.19.1 (2021-09-04) 22 | 23 | ### Bug fixes 24 | 25 | Fix the `plain` option, which was accidentally ignored before. 26 | 27 | Improve indentation of colon block syntax constructs. 28 | 29 | ## 0.19.0 (2021-09-03) 30 | 31 | ### Breaking changes 32 | 33 | First numbered release. 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (C) 2018-2021 by Marijn Haverbeke and others 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # @codemirror/lang-php [![NPM version](https://img.shields.io/npm/v/@codemirror/lang-php.svg)](https://www.npmjs.org/package/@codemirror/lang-php) 4 | 5 | [ [**WEBSITE**](https://codemirror.net/) | [**ISSUES**](https://github.com/codemirror/dev/issues) | [**FORUM**](https://discuss.codemirror.net/c/next/) | [**CHANGELOG**](https://github.com/codemirror/lang-php/blob/main/CHANGELOG.md) ] 6 | 7 | This package implements PHP language support for the 8 | [CodeMirror](https://codemirror.net/) code editor. 9 | 10 | The [project page](https://codemirror.net/) has more information, a 11 | number of [examples](https://codemirror.net/examples/) and the 12 | [documentation](https://codemirror.net/docs/). 13 | 14 | This code is released under an 15 | [MIT license](https://github.com/codemirror/lang-php/tree/main/LICENSE). 16 | 17 | We aim to be an inclusive, welcoming community. To make that explicit, 18 | we have a [code of 19 | conduct](http://contributor-covenant.org/version/1/1/0/) that applies 20 | to communication around the project. 21 | 22 | ## Usage 23 | 24 | ```javascript 25 | import {EditorView, basicSetup} from "codemirror" 26 | import {php} from "@codemirror/lang-php" 27 | 28 | const view = new EditorView({ 29 | parent: document.body, 30 | doc: ``, 31 | extensions: [basicSetup, php()] 32 | }) 33 | ``` 34 | 35 | ## API Reference 36 | 37 |
38 |
39 | php(config⁠?: Object = {}) → LanguageSupport
40 | 41 |

PHP language support.

42 |
43 | config
44 | 45 |
46 | baseLanguage⁠?: Language
47 | 48 |

By default, the parser will treat content outside of <? and 49 | ?> markers as HTML. You can pass a different language here to 50 | change that. Explicitly passing disables parsing of such content.

51 |
52 | plain⁠?: boolean
53 | 54 |

By default, PHP parsing only starts at the first <? marker. 55 | When you set this to true, it starts immediately at the start of 56 | the document.

57 |
58 |
59 | phpLanguage: LRLanguage
60 | 61 |

A language provider based on the Lezer PHP 62 | parser, extended with 63 | highlighting and indentation information.

64 |
65 |
66 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@codemirror/lang-php", 3 | "version": "6.0.1", 4 | "description": "PHP language support for the CodeMirror code editor", 5 | "scripts": { 6 | "test": "cm-runtests", 7 | "prepare": "cm-buildhelper src/php.ts" 8 | }, 9 | "keywords": [ 10 | "editor", 11 | "code" 12 | ], 13 | "author": { 14 | "name": "Marijn Haverbeke", 15 | "email": "marijn@haverbeke.berlin", 16 | "url": "http://marijnhaverbeke.nl" 17 | }, 18 | "type": "module", 19 | "main": "dist/index.cjs", 20 | "exports": { 21 | "import": "./dist/index.js", 22 | "require": "./dist/index.cjs" 23 | }, 24 | "types": "dist/index.d.ts", 25 | "module": "dist/index.js", 26 | "sideEffects": false, 27 | "license": "MIT", 28 | "dependencies": { 29 | "@codemirror/lang-html": "^6.0.0", 30 | "@codemirror/language": "^6.0.0", 31 | "@codemirror/state": "^6.0.0", 32 | "@lezer/common": "^1.0.0", 33 | "@lezer/php": "^1.0.0" 34 | }, 35 | "devDependencies": { 36 | "@codemirror/buildhelper": "^1.0.0" 37 | }, 38 | "repository": { 39 | "type": "git", 40 | "url": "https://github.com/codemirror/lang-php.git" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # @codemirror/lang-php [![NPM version](https://img.shields.io/npm/v/@codemirror/lang-php.svg)](https://www.npmjs.org/package/@codemirror/lang-php) 4 | 5 | [ [**WEBSITE**](https://codemirror.net/) | [**ISSUES**](https://github.com/codemirror/dev/issues) | [**FORUM**](https://discuss.codemirror.net/c/next/) | [**CHANGELOG**](https://github.com/codemirror/lang-php/blob/main/CHANGELOG.md) ] 6 | 7 | This package implements PHP language support for the 8 | [CodeMirror](https://codemirror.net/) code editor. 9 | 10 | The [project page](https://codemirror.net/) has more information, a 11 | number of [examples](https://codemirror.net/examples/) and the 12 | [documentation](https://codemirror.net/docs/). 13 | 14 | This code is released under an 15 | [MIT license](https://github.com/codemirror/lang-php/tree/main/LICENSE). 16 | 17 | We aim to be an inclusive, welcoming community. To make that explicit, 18 | we have a [code of 19 | conduct](http://contributor-covenant.org/version/1/1/0/) that applies 20 | to communication around the project. 21 | 22 | ## Usage 23 | 24 | ```javascript 25 | import {EditorView, basicSetup} from "codemirror" 26 | import {php} from "@codemirror/lang-php" 27 | 28 | const view = new EditorView({ 29 | parent: document.body, 30 | doc: ``, 31 | extensions: [basicSetup, php()] 32 | }) 33 | ``` 34 | 35 | ## API Reference 36 | 37 | @php 38 | 39 | @phpLanguage 40 | -------------------------------------------------------------------------------- /src/php.ts: -------------------------------------------------------------------------------- 1 | import {parser} from "@lezer/php" 2 | import {parseMixed} from "@lezer/common" 3 | import {html} from "@codemirror/lang-html" 4 | import {indentNodeProp, continuedIndent, delimitedIndent, foldNodeProp, foldInside, 5 | Language, LRLanguage, LanguageSupport} from "@codemirror/language" 6 | 7 | /// A language provider based on the [Lezer PHP 8 | /// parser](https://github.com/lezer-parser/php), extended with 9 | /// highlighting and indentation information. 10 | export const phpLanguage = LRLanguage.define({ 11 | name: "php", 12 | parser: parser.configure({ 13 | props: [ 14 | indentNodeProp.add({ 15 | IfStatement: continuedIndent({except: /^\s*({|else\b|elseif\b|endif\b)/}), 16 | TryStatement: continuedIndent({except: /^\s*({|catch\b|finally\b)/}), 17 | SwitchBody: context => { 18 | let after = context.textAfter, closed = /^\s*\}/.test(after), isCase = /^\s*(case|default)\b/.test(after) 19 | return context.baseIndent + (closed ? 0 : isCase ? 1 : 2) * context.unit 20 | }, 21 | ColonBlock: cx => cx.baseIndent + cx.unit, 22 | "Block EnumBody DeclarationList": delimitedIndent({closing: "}"}), 23 | ArrowFunction: cx => cx.baseIndent + cx.unit, 24 | "String BlockComment": () => null, 25 | Statement: continuedIndent({except: /^({|end(for|foreach|switch|while)\b)/}) 26 | }), 27 | foldNodeProp.add({ 28 | "Block EnumBody DeclarationList SwitchBody ArrayExpression ValueList": foldInside, 29 | ColonBlock(tree) { return {from: tree.from + 1, to: tree.to} }, 30 | BlockComment(tree) { return {from: tree.from + 2, to: tree.to - 2} } 31 | }) 32 | ] 33 | }), 34 | languageData: { 35 | commentTokens: {block: {open: "/*", close: "*/"}, line: "//"}, 36 | indentOnInput: /^\s*(?:case |default:|end(?:if|for(?:each)?|switch|while)|else(?:if)?|\{|\})$/, 37 | wordChars: "$", 38 | closeBrackets: {stringPrefixes: ["b", "B"]} 39 | } 40 | }) 41 | 42 | /// PHP language support. 43 | export function php(config: { 44 | /// By default, the parser will treat content outside of `` markers as HTML. You can pass a different language here to 46 | /// change that. Explicitly passing disables parsing of such content. 47 | baseLanguage?: Language | null, 48 | /// By default, PHP parsing only starts at the first ` { 64 | if (!node.type.isTop) return null 65 | return { 66 | parser: base!.parser, 67 | overlay: node => node.name == "Text" 68 | } 69 | }), 70 | top: config.plain ? "Program" : "Template" 71 | }), support) 72 | } 73 | --------------------------------------------------------------------------------