Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

visible whitespace #1802

Merged
merged 2 commits into from
Apr 20, 2022
Merged

Conversation

the-mikedavis
Copy link
Member

@the-mikedavis the-mikedavis commented Mar 12, 2022

Example with this rebased on top of #1796:

whitespace-with-indent-guides

Adds some configuration options to config.toml:

# render whitespace characters (\t, \n, ' ')
[editor.whitespace]
render = "all"
# render = "none"    to disable visible whitespace

# or control whether each character is shown:
[editor.whitespace.render]
space = "all"
tab = "all"
newline = "none"

# control the grapheme used to represent the whitespace
[editor.whitespace.characters]
space = "·"
tab = ""
newline = ""

And a new scope for themes ui.virtual.whitespace, which should be set to something slightly off-background.

This makes way for a future "editor.whitespace.render" = "selection" as well, which could enable showing whitespace only in a selection rather than always.

TODOs:

  • non breaking space character? (U+00A0F, default: )
  • update existing themes to include ui.virtual
  • add new config documentation to the book
  • decide whether this should be off by default
  • squash down commits 🔨

connects #1068
closes #1218
closes #688

book/src/themes.md Outdated Show resolved Hide resolved
helix-term/src/ui/editor.rs Outdated Show resolved Hide resolved
helix-view/src/editor.rs Outdated Show resolved Hide resolved
helix-view/src/editor.rs Outdated Show resolved Hide resolved
@EpocSquadron
Copy link
Contributor

I vote for on by default, this makes it fast easier to see when you are on the ending character, which will resolve a lot of confusion on how certain selection operations work. It suits the batteries included approach of helix better, IMO.

@Omnikar
Copy link
Contributor

Omnikar commented Mar 13, 2022

Thanks for continuing #1218. There's a bug I'd encountered when I first made that PR where when the cursor is at the very end of the file (after all characters in the file), the spot the cursor is on is rendered as a space; the bug seems to still be present here.

@the-mikedavis
Copy link
Member Author

oh interesting, I see it too:

trailing-space

@the-mikedavis
Copy link
Member Author

Looks like the space comes from here:

// `unwrap_or_else` part is for off-the-end indices of
// the rope, to allow cursor highlighting at the end
// of the rope.
let text = text.get_slice(start..end).unwrap_or_else(|| " ".into());

So I just pushed a commit that lets you configure which character that is and defaulted it to the braille blank (U+2800). It's a little brighter than a space, but I don't think it's very noticeable.

This approach seems kinda hacky though, what do you think?

@the-mikedavis
Copy link
Member Author

Also eof seems like the wrong word for it, I suppose it's more like a trailing_cursor

@the-mikedavis
Copy link
Member Author

actually it ended up being cleaner to detect when the highlight affected the trailing cursor and prevent substitution in that case: 45fd800

helix-term/src/ui/editor.rs Outdated Show resolved Hide resolved
helix-term/src/ui/editor.rs Outdated Show resolved Hide resolved
@archseer
Copy link
Member

I wouldn't want this on by default but it should be easy to toggle at runtime. I had space+s set up to toggle character highlighting on/off in vim so I could check indentation if I needed to.

@archseer
Copy link
Member

(So two config options: one for character map, one for on/off)

@sudormrfbin
Copy link
Member

I was also thinking about a generic toggle mode, so for example <space>ts for toggling whitespace, <space>td for toggling diagnostics, etc.

@the-mikedavis
Copy link
Member Author

the-mikedavis commented Mar 14, 2022

I added this to the setting command so we can say :set whitespace.render all with the default being off. We could simplify the whitespace.render config struct to a boolean but OTOH I'd like to support the whitespace.render = "selection" and other config in #1068 eventually

helix-view/src/editor.rs Outdated Show resolved Hide resolved
surface,
theme,
highlights,
&editor.config.whitespace,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just store this within editor?

Copy link
Member Author

@the-mikedavis the-mikedavis Mar 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think there's a method or two in Editor (other than this one) that end up using the config in other ways as well. Seems wise to make that available so we don't need to pass it in all the time.

edit: actually I don't think we can have the editor config or whitespace config be a field on the EditorView struct, this call to render_text_highlights looks static

let text = doc.text().slice(..);

let mut spans = Vec::new();
let mut visual_x = 0u16;
let mut line = 0u16;
let tab_width = doc.tab_width();
let tab = " ".repeat(tab_width);
let tab = if whitespace.render.tab() == WhitespaceRenderValue::All {
(1..tab_width).fold(whitespace.substitutions.tab.to_string(), |s, _| s + " ")
Copy link
Contributor

@pickfire pickfire Mar 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might need to check the tab substitution width, if it's substituted with two-width characters then it will look twice as large. But I doubt anyone would want to do that.

Copy link
Member Author

@the-mikedavis the-mikedavis Mar 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to have a multi-column char? If so, we might be able to check for this when deserializing from config with helix_core::graphemes::grapheme_width

#[serde(untagged, rename_all = "kebab-case")]
pub enum WhitespaceRender {
Basic(WhitespaceRenderValue),
Specific {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this Specific for? Why do we need to differentiate with basic?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comes from the original PR (#1218) - it's for this more advanced configuration: #1068 (comment)

I'm not too sure how valuable it is to have it be this configurable though. Maybe it'd be good enough to have a simple boolean that enables visible whitespace? You can also "turn off" visible whitespace for each kind of character by configuring substitutions to use spaces

[editor.whitespace.substitutions]
newline = " " # don't make newline visible

@pickfire
Copy link
Contributor

pickfire commented Mar 15, 2022

I was also thinking about a generic toggle mode, so for example ts for toggling whitespace, td for toggling diagnostics, etc.

@sudormrfbin IIRC doom emacs have some triggering mechanism as well similar to what you said, maybe can steal from them.

@the-mikedavis the-mikedavis marked this pull request as draft March 16, 2022 15:59
@archseer archseer mentioned this pull request Mar 20, 2022
1 task
@the-mikedavis the-mikedavis marked this pull request as ready for review March 25, 2022 02:01
@archseer archseer mentioned this pull request Mar 31, 2022
3 tasks
@the-mikedavis the-mikedavis force-pushed the md-visible-whitespace branch 2 times, most recently from d3ec589 to 3e2f361 Compare April 2, 2022 13:55
helix-term/src/ui/editor.rs Outdated Show resolved Hide resolved
@the-mikedavis the-mikedavis force-pushed the md-visible-whitespace branch 2 times, most recently from f168b4c to e40efd8 Compare April 9, 2022 19:18
@sudormrfbin sudormrfbin mentioned this pull request Apr 17, 2022
Co-authored-by: Michael Davis <mcarsondavis@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

toggle-whitespace command and associated keymap
6 participants