├── .gitignore ├── .github └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── package.json ├── rainbowBrackets.js ├── README.md └── contributing └── contributing.md /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | node_modules/ 3 | .DS_Store -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "🐛 Bug Report" 3 | about: Report a reproducible bug or regression. 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | ## Current Behavior 11 | 12 | 13 | 14 | ## Expected Behavior 15 | 16 | 17 | 18 | ## Steps to Reproduce the Problem 19 | 20 | 1. 21 | 1. 22 | 1. 23 | 24 | ## Environment 25 | 26 | - Version: 27 | - Platform: 28 | - Node.js Version: -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rainbowbrackets", 3 | "version": "2.0.2", 4 | "description": "colorizes brackets, parentheses and braces to help track scope", 5 | "main": "rainbowBrackets.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "build": "webpack" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/eriknewland/rainbowbrackets.git" 13 | }, 14 | "keywords": [ 15 | "rainbowbrackets", 16 | "codemirror", 17 | "codemirror6" 18 | ], 19 | "author": "Erik Newland", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/eriknewland/rainbowbrackets/issues" 23 | }, 24 | "homepage": "https://github.com/eriknewland/rainbowbrackets#readme", 25 | "dependencies": { 26 | "@codemirror/view": "^6.9.5" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 🌈 Feature request 3 | about: Suggest an amazing new idea for this project 4 | title: '' 5 | labels: enhancement 6 | assignees: '' 7 | 8 | --- 9 | 10 | ## Feature Request 11 | 12 | **Is your feature request related to a problem? Please describe.** 13 | 14 | 15 | **Describe the solution you'd like** 16 | 17 | 18 | **Describe alternatives you've considered** 19 | 20 | 21 | ## Are you willing to resolve this issue by submitting a Pull Request? 22 | 23 | 26 | 27 | - [ ] Yes, I have the time, and I know how to start. 28 | - [ ] Yes, I have the time, but I don't know how to start. I would need guidance. 29 | - [ ] No, I don't have the time, although I believe I could do it if I had the time... 30 | - [ ] No, I don't have the time and I wouldn't even know how to start. 31 | 32 | -------------------------------------------------------------------------------- /rainbowBrackets.js: -------------------------------------------------------------------------------- 1 | import { EditorView, Decoration, ViewPlugin } from '@codemirror/view'; 2 | 3 | function generateColors() { 4 | return [ 5 | 'red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet', 6 | ]; 7 | } 8 | 9 | const rainbowBracketsPlugin = ViewPlugin.fromClass(class { 10 | decorations; 11 | 12 | constructor(view) { 13 | this.decorations = this.getBracketDecorations(view); 14 | } 15 | 16 | update(update) { 17 | if (update.docChanged || update.selectionSet || update.viewportChanged) { 18 | this.decorations = this.getBracketDecorations(update.view); 19 | } 20 | } 21 | 22 | getBracketDecorations(view) { 23 | const { doc } = view.state; 24 | const decorations = []; 25 | const stack = []; 26 | const colors = generateColors(); 27 | 28 | for (let pos = 0; pos < doc.length; pos += 1) { 29 | const char = doc.sliceString(pos, pos + 1); 30 | if (char === '(' || char === '[' || char === '{') { 31 | stack.push({ type: char, from: pos }); 32 | } else if (char === ')' || char === ']' || char === '}') { 33 | const open = stack.pop(); 34 | if (open && open.type === this.getMatchingBracket(char)) { 35 | const color = colors[stack.length % colors.length]; 36 | decorations.push( 37 | Decoration.mark({ class: `rainbow-bracket-${color}` }).range(open.from, open.from + 1), 38 | Decoration.mark({ class: `rainbow-bracket-${color}` }).range(pos, pos + 1), 39 | ); 40 | } 41 | } 42 | } 43 | 44 | decorations.sort((a, b) => a.from - b.from || a.startSide - b.startSide); 45 | 46 | return Decoration.set(decorations); 47 | } 48 | 49 | // eslint-disable-next-line class-methods-use-this 50 | getMatchingBracket(closingBracket) { 51 | switch (closingBracket) { 52 | case ')': return '('; 53 | case ']': return '['; 54 | case '}': return '{'; 55 | default: return null; 56 | } 57 | } 58 | }, { 59 | decorations: (v) => v.decorations, 60 | }); 61 | 62 | export default function rainbowBrackets() { 63 | return [ 64 | rainbowBracketsPlugin, 65 | EditorView.baseTheme({ 66 | '.rainbow-bracket-red': { color: 'red' }, 67 | '.rainbow-bracket-red > span': { color: 'red' }, 68 | '.rainbow-bracket-orange': { color: 'orange' }, 69 | '.rainbow-bracket-orange > span': { color: 'orange' }, 70 | '.rainbow-bracket-yellow': { color: 'yellow' }, 71 | '.rainbow-bracket-yellow > span': { color: 'yellow' }, 72 | '.rainbow-bracket-green': { color: 'green' }, 73 | '.rainbow-bracket-green > span': { color: 'green' }, 74 | '.rainbow-bracket-blue': { color: 'blue' }, 75 | '.rainbow-bracket-blue > span': { color: 'blue' }, 76 | '.rainbow-bracket-indigo': { color: 'indigo' }, 77 | '.rainbow-bracket-indigo > span': { color: 'indigo' }, 78 | '.rainbow-bracket-violet': { color: 'violet' }, 79 | '.rainbow-bracket-violet > span': { color: 'violet' }, 80 | }), 81 | ]; 82 | } 83 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![new hero](https://user-images.githubusercontent.com/114263701/232826474-ba2778e6-c37f-4146-bbcc-bba9f92fb744.png) 2 | # rainbowBrackets: a codeMirror Extension 3 | --- 4 | 5 | This is a working attempt at a rainbowBracket extension for CodeMirror6. 6 | 7 | ### What are rainbow brackets? 8 | Great question! Rainbow Brackets are brackets, curly braces, or parentheses that change color with scope/depth. See intelliJ's Rainbow Bracket Extension. 9 | 10 | 11 | ## Demo 12 | 13 | ![rainbow-bracket-demo-npm](https://user-images.githubusercontent.com/114263701/233111574-44b592af-70cb-4330-b65f-9410562d04ce.gif) 14 | 15 | Live Implementation: 16 | 17 | [CodePen GPT](https://codepengpt.netlify.app/) 18 | 19 | ## Table of Contents 20 | 21 | - [rainbowBrackets: a codeMirror Extension](#rainbowbrackets-a-codemirror-extension) 22 | - [What are rainbow brackets?](#what-are-rainbow-brackets) 23 | - [Demo](#demo) 24 | - [Features](#features) 25 | - [Usage/Examples](#usageexamples) 26 | - [@uiw/react-codemirror](#uiwreact-codemirror) 27 | - [codemirror package](#codemirror-package) 28 | - [vanilla javascript](#vanilla-javascript) 29 | - [API Reference](#api-reference) 30 | - [Deployment](#deployment) 31 | - [Roadmap](#roadmap) 32 | - [FAQ](#faq) 33 | - [Does this work for controlled and uncontroleld CodeMirror Components?](#does-this-work-for-controlled-and-uncontroleld-codemirror-components) 34 | - [Can I change the colors?](#can-i-change-the-colors) 35 | - [Contributing](#contributing) 36 | - [License](#license) 37 | - [Related](#related) 38 | - [Closing](#closing) 39 | 40 | ## Features 41 | 42 | - Depth/Scope detection 43 | - 7 customizable bracket classes 44 | - Default colors: Red Orange Yellow Green Blue Indigo Violet 45 | - Implementation agnostic 46 | - Theme agnostic 47 | 48 | 49 | ## Usage/Examples 50 | 51 | Depending on your theme and extensions, the rainbowBrackets may not apply correctly as the span structure looks something like this, where the color of the inner span will override the rainbow-bracket. Investigate with Chrome Dev Tools if you're encountering a bug. 52 | 53 | >Nearly Fixed as of 2.0.0. See updated API Reference. 54 | 55 | ```javascript 56 | 57 | { //opening bracket displaying theme color 58 | 59 | ``` 60 | 61 | To work around this, we can add the following CSS. 62 | ```css 63 | .rainbow-bracket-red > span { 64 | color: red; 65 | } 66 | 67 | .rainbow-bracket-orange > span { 68 | color: orange; 69 | } 70 | 71 | .rainbow-bracket-yellow > span { 72 | color: yellow; 73 | } 74 | 75 | .rainbow-bracket-green > span { 76 | color: green; 77 | } 78 | 79 | .rainbow-bracket-blue > span { 80 | color: blue; 81 | } 82 | 83 | .rainbow-bracket-indigo > span { 84 | color: indigo; 85 | } 86 | 87 | .rainbow-bracket-violet > span { 88 | color: violet; 89 | } 90 | ``` 91 | This will override the inner span. Future implementations should ensure the rainbowBracket has higher precedence. 92 | 93 | ### @uiw/react-codemirror 94 | 95 | ```javascript 96 | import rainbowBrackets from 'rainbowbrackets' 97 | 98 | const cmExtensions = [/*Your Other Extensions */, rainbowBrackets()] 99 | 100 | //... 101 | 102 | 106 | ``` 107 | ### codemirror package 108 | ``` 109 | coming soon...handle as you would an existing extension 110 | ``` 111 | ### vanilla javascript 112 | ``` 113 | coming soon...handle as you would an existing extension 114 | ``` 115 | ## API Reference 116 | 117 | #### Class Structure 118 | ##### 1.0.1 119 | 120 | ```javascript 121 | .rainbow-bracket-red {color: 'red'} 122 | .rainbow-bracket-orange {color: 'orange'} 123 | .rainbow-bracket-yellow {color: 'yellow'} 124 | .rainbow-bracket-green {color: 'green'} 125 | .rainbow-bracket-blue {color: 'blue'} 126 | .rainbow-bracket-indigo {color: 'indigo'} 127 | .rainbow-bracket-violet {color: 'violet'} 128 | ``` 129 | 130 | ##### 2.0.0 131 | ```javascript 132 | .rainbow-bracket-red: { color: 'red' } 133 | .rainbow-bracket-red > span: { color: 'red' } 134 | .rainbow-bracket-orange: { color: 'orange' } 135 | .rainbow-bracket-orange > span: { color: 'orange' } 136 | .rainbow-bracket-yellow: { color: 'yellow' } 137 | .rainbow-bracket-yellow > span: { color: 'yellow' } 138 | .rainbow-bracket-green: { color: 'green' } 139 | .rainbow-bracket-green > span: { color: 'green' } 140 | .rainbow-bracket-blue: { color: 'blue' } 141 | .rainbow-bracket-blue > span: { color: 'blue' } 142 | .rainbow-bracket-indigo: { color: 'indigo' } 143 | .rainbow-bracket-indigo > span: { color: 'indigo' } 144 | .rainbow-bracket-violet: { color: 'violet' } 145 | .rainbow-bracket-violet > span: { color: 'violet' } 146 | ``` 147 | 148 | ## Development 149 | 150 | git fork, clone, then navigate into this repository. 151 | 152 | Install dependencies: 153 | 154 | ```bash 155 | npm install 156 | ``` 157 | 158 | See [Contributing](#contributing). TLDR: Make a branch with your feature name/bug fix, include detailed commit log and PR message. 159 | 160 | ## Roadmap 161 | 162 | - More testing across a wide range of functions, scopes, languages, etc 163 | 164 | ## FAQ 165 | 166 | #### Does this work for controlled and uncontroleld CodeMirror Components? 167 | 168 | Yes! Although it's likely there are unknown bugs I have yet to encounter. 169 | 170 | #### Can I change the colors? 171 | 172 | Yes! See the API reference above. If you want to change red to pink, simply use a little CSS: 173 | ```css 174 | .rainbow-bracket-red { 175 | color: pink; 176 | } 177 | /* Override may be neccessary as well*/ 178 | .rainbow-bracket-red > span { 179 | color: pink; 180 | } 181 | 182 | ``` 183 | I'll rename these classes if enough people want color flexibility to something more intuitive. 184 | 185 | 186 | ## Contributing 187 | 188 | See [contributing.md](contributing/contributing.md) to get started. 189 | 190 | Please adhere to this project's `code of conduct`. 191 | 192 | 193 | ## License 194 | 195 | [MIT](https://choosealicense.com/licenses/mit/) 196 | 197 | 198 | ## Related 199 | 200 | [CodeMirror HomePage](https://codemirror.net/) 201 | 202 | [CodeMirror Extensions](https://codemirror.net/docs/extensions/) 203 | 204 | [CodeMirror Extensions Forum Discussion](https://discuss.codemirror.net/t/list-of-community-extensions/4899) 205 | 206 | [CodeMirror-React](https://www.npmjs.com/package/@uiw/react-codemirror) 207 | 208 | [IntelliJ RainbowBrackets](https://plugins.jetbrains.com/plugin/10080-rainbow-brackets) 209 | 210 | ## Closing 211 | 212 | This extension is still experimental and it may not perfectly colorize scope. It still may be useful, or at least fun to use. Improvements are welcome. Big thank you to Marijn Haverbeke, the CodeMirror community, and all its contributors. -------------------------------------------------------------------------------- /contributing/contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to rainbowBrackets 2 | 3 | First off, thanks for taking the time to contribute! ❤️ 4 | 5 | All types of contributions are encouraged and valued. See the [Table of Contents](#table-of-contents) for different ways to help and details about how this project handles them. Please make sure to read the relevant section before making your contribution. It will make it a lot easier for us maintainers and smooth out the experience for all involved. The community looks forward to your contributions. 🎉 6 | 7 | > And if you like the project, but just don't have time to contribute, that's fine. There are other easy ways to support the project and show your appreciation, which we would also be very happy about: 8 | > - Star the project 9 | > - Tweet about it 10 | > - Refer this project in your project's readme 11 | > - Mention the project at local meetups and tell your friends/colleagues 12 | 13 | 14 | ## Table of Contents 15 | 16 | - [Code of Conduct](#code-of-conduct) 17 | - [I Have a Question](#i-have-a-question) 18 | - [I Want To Contribute](#i-want-to-contribute) 19 | - [Reporting Bugs](#reporting-bugs) 20 | - [Suggesting Enhancements](#suggesting-enhancements) 21 | - [Your First Code Contribution](#your-first-code-contribution) 22 | - [Improving The Documentation](#improving-the-documentation) 23 | - [Styleguides](#styleguides) 24 | - [Commit Messages](#commit-messages) 25 | - [Join The Project Team](#join-the-project-team) 26 | 27 | 28 | ## Code of Conduct 29 | 30 | Practice kindness, respect and patience. 31 | 32 | ## I Have a Question 33 | 34 | > If you want to ask a question, we assume that you have read the available [CodeMirror Documentation](https://codemirror.net/docs/) and have familiarized yourself with how extensions work in CodeMirror6. I specifically recommend the CodeMirror forum for discussion of common topics/goals. 35 | 36 | If you then still have a question and need clarification: 37 | 38 | - Open an [Issue](/issues/new). 39 | - Provide as much context as you can about what you're running into. 40 | - Provide project and platform versions (nodejs, npm, etc), depending on what seems relevant. 41 | 42 | ## I Want To Contribute 43 | 44 | See [BUG_REPORT.md](BUG_REPORT.md) and [PULL_REQUEST.md](PULL_REQUEST.md) for ways to get started. 45 | 46 | > ### Legal Notice 47 | > When contributing to this project, you must agree that you have authored 100% of the content, that you have the necessary rights to the content and that the content you contribute may be provided under the project license. 48 | 49 | ### Reporting Bugs 50 | 51 | 52 | #### Before Submitting a Bug Report 53 | 54 | A good bug report shouldn't leave others needing to chase you down for more information. Therefore, we ask you to investigate carefully, collect information and describe the issue in detail in your report. Please complete the following steps in advance to help us fix any potential bug as fast as possible. 55 | 56 | - Make sure that you are using the latest version. 57 | - Determine if your bug is really a bug and not an error on your side e.g. using incompatible environment components/versions (Make sure that you have read the [CodeMirror Documentation](https://codemirror.net/docs/)). 58 | - Double check if this error is already being addressed via active issue. 59 | - Also make sure to search the internet (including Stack Overflow) to see if users outside of the GitHub community have discussed the issue. 60 | - Collect information about the bug: 61 | - Stack trace (Traceback) 62 | - OS, Platform and Version (Windows, Linux, macOS, x86, ARM) 63 | - Version of the interpreter, compiler, SDK, runtime environment, package manager, depending on what seems relevant. 64 | - Possibly your input and the output 65 | - Can you reliably reproduce the issue? And can you also reproduce it with older versions? 66 | 67 | 68 | #### How Do I Submit a Good Bug Report? 69 | 70 | We use GitHub issues to track bugs and errors. If you run into an issue with the project: 71 | 72 | - Open an [Issue](/issues/new). 73 | - Explain the behavior you would expect and the actual behavior. 74 | - Please provide as much context as possible and describe the *reproduction steps* that someone else can follow to recreate the issue on their own. This usually includes your code. For good bug reports you should isolate the problem and create a reduced test case. 75 | - Provide the information you collected in the previous section. 76 | 77 | Once it's filed: 78 | 79 | - I (or hopefully a contributor one day!) will investigate. 80 | - A team member will try to reproduce the issue with your provided steps. If there are no reproduction steps or no obvious way to reproduce the issue, the team will ask you for those steps and mark the issue as `needs-repro`. Bugs with the `needs-repro` tag will not be addressed until they are reproduced. 81 | - If the team is able to reproduce the issue, it will be marked `needs-fix`, as well as possibly other tags (such as `critical`), and the issue will be addressed shortly. 82 | 83 | ### Suggesting Enhancements 84 | 85 | This section guides you through submitting an enhancement suggestion for rainbowBrackets, **including completely new features and minor improvements to existing functionality**. Following these guidelines will help maintainers and the community to understand your suggestion and find related suggestions. 86 | 87 | 88 | #### Before Submitting an Enhancement 89 | 90 | - Make sure that you are using the latest version. 91 | - Read the [documentation]() carefully and find out if the functionality is already covered, maybe by an individual configuration. 92 | - Perform a [search](/issues) to see if the enhancement has already been suggested. If it has, add a comment to the existing issue instead of opening a new one. 93 | - Find out whether your idea fits with the scope and aims of the project. It's up to you to make a strong case to convince the project's developers of the merits of this feature. Keep in mind that we want features that will be useful to the majority of our users and not just a small subset. If you're just targeting a minority of users, consider writing an add-on/plugin library. 94 | - To expand on the above, the beauty of CodeMirror is its customization and modularity - if you believe your enhancement to be worthy of its own extension, simply publish it as such. 95 | 96 | #### How Do I Submit a Good Enhancement Suggestion? 97 | 98 | Enhancement suggestions are tracked as [GitHub issues](/issues). 99 | 100 | - Use a **clear and descriptive title** for the issue to identify the suggestion. 101 | - Provide a **step-by-step description of the suggested enhancement** in as many details as possible. 102 | - **Describe the current behavior** and **explain which behavior you expected to see instead** and why. At this point you can also tell which alternatives do not work for you. 103 | - You may want to **include screenshots and animated GIFs** which help you demonstrate the steps or point out the part which the suggestion is related to. You can use [this tool](https://www.cockos.com/licecap/) to record GIFs on macOS and Windows, and [this tool](https://github.com/colinkeenan/silentcast) or [this tool](https://github.com/GNOME/byzanz) on Linux. 104 | - **Explain why this enhancement would be useful** to most rainbowBrackets users. You may also want to point out the other projects that solved it better and which could serve as inspiration. 105 | 106 | ## Attribution 107 | These contribution guidelines are based on **contributing.md**. [Make your own](https://contributing.md/)! 108 | --------------------------------------------------------------------------------