Skip to content
This repository was archived by the owner on Apr 1, 2020. It is now read-only.

Conversation

akinsho
Copy link
Member

@akinsho akinsho commented Jul 8, 2018

Having had a go at implementing a layer in #2344, I've also implemented a layer to highlight css colors in certain files. The way that this works is that if a file extension is in the whitelist any hex/rgba/hsla color is colored.

screen shot 2018-07-08 at 23 10 12

Todos:

  • Experimental flag and user config options for file extensions
  • Doesn't match the css color names aka rebeccapurple, goldenrod etc.
  • Ci Test

Outstanding Issues:
- The layer renders on top of the renderer so the color highlight lies on top of the text obscuring the syntax highlight, currently I reduce the colors opacity but this actually changes the color so its not quite accurate
- Potentially I could put the matching color into the element but set the color prop to transparent so it makes a word shaped window 😆
- Another idea would be just to render the text inside the highlight using the user's font

  • The regex tries to fuzzy match inside a string this can lead to ?rare situations where things are incorrectly highlighted - for example html entities can contain what the regex would recognise as a hex string.

This is a tough one as its beyond my abilities with regex to not break the regex but have it fail if it matches inside another word I think possibly using some sort of word boundary \\b check might work but would rather not try and fix this right off the bat as its a potential rabbit hole.

screen shot 2018-07-08 at 20 57 32

@codecov
Copy link

codecov bot commented Jul 8, 2018

Codecov Report

Merging #2413 into master will increase coverage by 0.02%.
The diff coverage is 75%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2413      +/-   ##
==========================================
+ Coverage   38.23%   38.26%   +0.02%     
==========================================
  Files         300      300              
  Lines       12530    12537       +7     
  Branches     1650     1650              
==========================================
+ Hits         4791     4797       +6     
- Misses       7484     7485       +1     
  Partials      255      255
Impacted Files Coverage Δ
...src/Services/Configuration/DefaultConfiguration.ts 87.5% <ø> (ø) ⬆️
browser/src/Editor/NeovimEditor/NeovimSurface.tsx 65.21% <100%> (+1.58%) ⬆️
browser/src/UI/Shell/OverlayView.tsx 60% <100%> (+4.44%) ⬆️
...src/Editor/NeovimEditor/NeovimBufferLayersView.tsx 44.44% <50%> (+1.58%) ⬆️
browser/src/UI/components/common.ts 78.26% <75%> (-0.32%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 02dd6f2...6553138. Read the comment docs.

@akinsho akinsho changed the title [WIP] Feature/Color layer Feature/Color layer Jul 9, 2018
@akinsho akinsho requested review from CrossR and bryphe July 9, 2018 12:25
@CrossR
Copy link
Member

CrossR commented Jul 9, 2018

The regex tries to fuzzy match inside a string this can lead to ?rare situations where things are incorrectly highlighted - for example html entities can contain what the regex would recognise as a hex string.

I think leaving this for a second PR makes sense, but just in case its useful, have you considered trying to use the text mate grammar scopes for this? You could potentially restrict it to just strings etc.

I've just given it a try, and looks good! Only two comments I have are potentially adding some error checking, and how the layers are applied to the editor, though I expect that is an API question.

I added .vim to my list of filetypes for this, and upon opening the onedark.vim file, the editor crashed to the "Oh no!" screen. I probably shouldn't have tried opening a different filetype, but I also wouldn't have expected the editor to die.

The second point is more about the API I think, but due to this being a layer that sits on top, the cursor is hidden when it enters the string, meaning you can't actually see where you are in the string. (I can just about see the cursor since the highlight box doesn't seem to be exactly aligned with the cursor, but still).

@akinsho
Copy link
Member Author

akinsho commented Jul 9, 2018

@CrossR thanks for trying this out 👍, tbh for this first thought was using a grammar but tbh I think it will make this process way more expensive I created what should be fairly multi purpose tokenForLine fn when doing the hover syntax highlighting and tbh the process is async calling in a grammar for the language and then it would involve parsing the entire string then looking for a color token which tbh could have a variety of tokens assigned to it depending on the language.

I think this regex is fairly sound but just needs a tweak for the edge case, it could turn out to not be the case in the long term but for now the process is way more straightforward, I can have a go later on to see how it can be tweaked.

Re. the cursor thats a very good point I didn't realise that. I didn't put a z-index on the highlights but I imagine the cursor sits over things in a very similar manner so can be covered, maybe it could hopefully be as simple as assigning it a high z-index

Re. the vim filetype error can I ask what the error was I'm not sure what case would have caused the error there the function should only run if the file hits any color matches

@CrossR
Copy link
Member

CrossR commented Jul 9, 2018

I added .vim to the list of extensions and opened onedark.vim in the repo and get the following:

image

I can get the output from the un-minified build in a bit if you aren't able to reproduce it.

Weirdly, solarized.vim worked fine, so it must be some colours they have defined just in one dark.

@akinsho
Copy link
Member Author

akinsho commented Jul 9, 2018

@CrossR thanks that helps pretty sure it might be that the colors that are defined are defined with caps so it correctly matches them but the color that is passed to the highlight is Magenta instead of magenta which isn't valid css

@akinsho
Copy link
Member Author

akinsho commented Jul 9, 2018

Pushed a fix and tested it out in onedark.vim seems to have resolved it was down to the color names also just added a try catch to the whole thing so it doesn't throw errors all the way up.

Edit: Also upped the z-index on the cursor so it lies above the highlight unfortunately they aren't flush with one another but think its fine for now'

@akinsho
Copy link
Member Author

akinsho commented Jul 9, 2018

Small explainer of added changes slightly unrelated changes, because of the cursor hiding issue I added a StackLayer styled component that takes a z-index as an optional argument, i've set the cursor to have a z-index of 2 and the overlay to have one of 3, the overlay is higher because the wild menu etc should cover the cursor but without a higher zIndex the cursor pokes through

@bryphe
Copy link
Member

bryphe commented Jul 10, 2018

Just got a chance to test this out! And tweeted about it 😄

Looks awesome @akin909 ! And thanks for your help testing it, @CrossR ! Nice catch on the crash with onedark.vim and the cursor issue.

One thing we'll have to keep in mind with the buffer layers is performance (they have the potential to cost extra perf, since their rendering is triggered frequently - ie, anytime the cursor position changes). So far it seems like we're in good shape - ran this through the profiler (and I also have indent guides), and I didn't see much impact. No action needed there, just something I was thinking about.

Really appreciate the test, too @akin909 ! Looks good to me! 💯

@akinsho akinsho merged commit 75ab1b8 into onivim:master Jul 10, 2018
@akinsho akinsho deleted the feature/color-layer branch July 10, 2018 07:39
@akinsho akinsho restored the feature/color-layer branch October 1, 2018 09:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants