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

customizable file type mapping #3273

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The following elements can be configured:
| `file-name` | The path/name of the opened file |
| `file-encoding` | The encoding of the opened file if it differs from UTF-8 |
| `file-line-ending` | The file line endings (CRLF or LF) |
| `file-type` | The type of the opened file |
| `file-type` | The type of the opened file (see `editor.file-type-indicators` below) |
| `diagnostics` | The number of warnings and/or errors |
| `selections` | The number of active selections |
| `position` | The cursor position |
Expand Down Expand Up @@ -223,3 +223,19 @@ Example:
render = true
character = "╎"
```

### `[editor.file-type-indicators]` Section

Remaps / overrides the standard file type strings to custom strings. If a file type is
defined in this section, the string value assigned will be used instead of the default.
Any file types not defined in this section will use the default file type string.
Example:

```toml
[editor.file-type-indicators]
"rust" = "rs"
"typescript" = "ts"
```
In the above example, Rust and TypeScript files will use "rs" and "ts" as their respective
file type indicators, while JavaScript and others not defined will use the Helix default.
Currently, the file type indicator string is used only by the `file-type` statusline element.
6 changes: 6 additions & 0 deletions helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let file_type = context.doc.language_id().unwrap_or("text");
Copy link
Member

Choose a reason for hiding this comment

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

Somewhat unrelated to this change, I think this might be better off as the scope field from languages.toml which is context.doc.language().unwrap_or("text"). That way Rust is "source.rust", toml is "source.toml", html is "text.html.basic", etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you suggest any changes to this PR? My opinion is that we should have a function whose sole purpose is to return the language. Right now there are several possible ways to do it, each with their own issues.

Copy link
Member

Choose a reason for hiding this comment

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

we should have a function whose sole purpose is to return the language

The trouble is: what is the language? We could say it's the scope or we could say it's the name (language_id but not language_id(), see also #3338). It depends on what we want the public API of language names to be: is the LanguageConfiguration.language_id an implementation detail or is it ok for configurations to use them?

I'd be hesitant to merge this PR until we figure that out since this PR introduces a way for configurations to depend on language names/ids/scopes.

@archseer what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This PR merely provides a mapping from the current language reporting functionality so that we can use custom strings that don't take up 1/4 of the status line. I'd be really disappointed if this PR was held up as a result of a feature that is still unimplemented.

let editor_config = context.editor.config();
let file_type = editor_config
.file_type_indicators
.get(file_type)
.map(|x| x.as_str())
.unwrap_or(file_type);
sbromberger marked this conversation as resolved.
Show resolved Hide resolved

write(context, format!(" {} ", file_type), None);
}
Expand Down
3 changes: 3 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ pub struct Config {
pub indent_guides: IndentGuidesConfig,
/// Whether to color modes with different colors. Defaults to `false`.
pub color_modes: bool,
/// File type icon/string map for overriding file type.
pub file_type_indicators: HashMap<String, String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -499,6 +501,7 @@ impl Default for Config {
whitespace: WhitespaceConfig::default(),
indent_guides: IndentGuidesConfig::default(),
color_modes: false,
file_type_indicators: HashMap::new(),
}
}
}
Expand Down