Skip to content

Commit a9ea3ad

Browse files
committed
Rework: removed language specific file size in favor of global config
This sets a default maximum size of 150mb to trigger syntax highlighting while allowing the user to specify an editor setting `syntax-max-file-size` to change the value of the maximum size it will also notify the user once a file is opened if the limit is exceeded.
1 parent 62364f1 commit a9ea3ad

File tree

7 files changed

+31
-18
lines changed

7 files changed

+31
-18
lines changed

book/src/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ on unix operating systems.
5757
| `rulers` | List of column positions at which to display the rulers. Can be overridden by language specific `rulers` in `languages.toml` file. | `[]` |
5858
| `bufferline` | Renders a line at the top of the editor displaying open buffers. Can be `always`, `never` or `multiple` (only shown if more than one buffer is in use) | `never` |
5959
| `color-modes` | Whether to color the mode indicator with different colors depending on the mode itself | `false` |
60+
| `syntax-max-file-size` | Specify the maximum file size in bytes to enable syntax highlighting | `150000000` |
6061

6162
### `[editor.statusline]` Section
6263

book/src/languages.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ These configuration keys are available:
6262
| `grammar` | The tree-sitter grammar to use (defaults to the value of `name`) |
6363
| `formatter` | The formatter for the language, it will take precedence over the lsp when defined. The formatter must be able to take the original file as input from stdin and write the formatted file to stdout |
6464
| `max-line-length` | Maximum line length. Used for the `:reflow` command |
65-
| `hl-max-size` | Maximum file size in bytes for syntax highlighting |
6665

6766
### File-type detection and the `file-types` key
6867

helix-core/src/syntax.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ pub struct LanguageConfiguration {
122122
#[serde(default, skip_serializing, deserialize_with = "deserialize_auto_pairs")]
123123
pub auto_pairs: Option<AutoPairs>,
124124

125-
pub rulers: Option<Vec<u16>>, // if set, override editor's rulers
126-
pub hl_max_size: Option<usize>, // maximum file size in bytes for highlighting
125+
pub rulers: Option<Vec<u16>>, // if set, override editor's rulers
127126
}
128127

129128
#[derive(Debug, PartialEq, Eq, Hash)]
@@ -551,7 +550,6 @@ pub struct Loader {
551550
language_config_ids_by_extension: HashMap<String, usize>, // Vec<usize>
552551
language_config_ids_by_suffix: HashMap<String, usize>,
553552
language_config_ids_by_shebang: HashMap<String, usize>,
554-
555553
scopes: ArcSwap<Vec<String>>,
556554
}
557555

helix-term/src/application.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl Application {
176176
&config.editor
177177
})),
178178
);
179-
179+
let mut enable_syntax_highlighting = true;
180180
let keys = Box::new(Map::new(Arc::clone(&config), |config: &Config| {
181181
&config.keys
182182
}));
@@ -223,11 +223,18 @@ impl Application {
223223
// opened last is focused on.
224224
let view_id = editor.tree.focus;
225225
let doc = doc_mut!(editor, &doc_id);
226+
enable_syntax_highlighting = doc.enable_syntax_highlighting;
226227
let pos = Selection::point(pos_at_coords(doc.text().slice(..), pos, true));
227228
doc.set_selection(view_id, pos);
228229
}
229230
}
230-
editor.set_status(format!("Loaded {} files.", nr_of_files));
231+
if !enable_syntax_highlighting {
232+
editor.set_error(format!(
233+
"Warning: Syntax highlighting is disabled due to exceeding the size limit"
234+
));
235+
} else {
236+
editor.set_status(format!("Loaded {} files.", nr_of_files));
237+
}
231238
// align the view to center after all files are loaded,
232239
// does not affect views without pos since it is at the top
233240
let (view, doc) = current!(editor);

helix-term/src/ui/picker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl<T: Item> FilePicker<T> {
150150
(size, _) if size > MAX_FILE_SIZE_FOR_PREVIEW => CachedPreview::LargeFile,
151151
_ => {
152152
// TODO: enable syntax highlighting; blocked by async rendering
153-
Document::open(path, None, None)
153+
Document::open(path, None, None, 0)
154154
.map(|doc| CachedPreview::Document(Box::new(doc)))
155155
.unwrap_or(CachedPreview::NotFound)
156156
}

helix-view/src/document.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ pub struct Document {
133133

134134
diagnostics: Vec<Diagnostic>,
135135
language_server: Option<Arc<helix_lsp::Client>>,
136+
pub enable_syntax_highlighting: bool,
136137
}
137138

138139
use std::{fmt, mem};
@@ -371,6 +372,7 @@ impl Document {
371372
last_saved_revision: 0,
372373
modified_since_accessed: false,
373374
language_server: None,
375+
enable_syntax_highlighting: true,
374376
}
375377
}
376378

@@ -381,6 +383,7 @@ impl Document {
381383
path: &Path,
382384
encoding: Option<&'static encoding::Encoding>,
383385
config_loader: Option<Arc<syntax::Loader>>,
386+
file_size_limit: usize,
384387
) -> Result<Self, Error> {
385388
// Open the file if it exists, otherwise assume it is a new file (and thus empty).
386389
let (rope, encoding) = if path.exists() {
@@ -397,6 +400,9 @@ impl Document {
397400
// set the path and try detecting the language
398401
doc.set_path(Some(path))?;
399402
if let Some(loader) = config_loader {
403+
if doc.text().len_bytes() > file_size_limit {
404+
doc.enable_syntax_highlighting = false;
405+
}
400406
doc.detect_language(loader);
401407
}
402408

@@ -681,17 +687,12 @@ impl Document {
681687
loader: Option<Arc<helix_core::syntax::Loader>>,
682688
) {
683689
if let (Some(language_config), Some(loader)) = (language_config, loader) {
684-
if let Some(max_size) = language_config.hl_max_size {
685-
if self.text.len_bytes() > max_size {
686-
self.syntax = None;
687-
self.language = Some(language_config);
688-
return;
690+
if self.enable_syntax_highlighting {
691+
if let Some(highlight_config) = language_config.highlight_config(&loader.scopes()) {
692+
let syntax = Syntax::new(&self.text, highlight_config, loader);
693+
self.syntax = Some(syntax);
689694
}
690695
}
691-
if let Some(highlight_config) = language_config.highlight_config(&loader.scopes()) {
692-
let syntax = Syntax::new(&self.text, highlight_config, loader);
693-
self.syntax = Some(syntax);
694-
}
695696

696697
self.language = Some(language_config);
697698
} else {

helix-view/src/editor.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ pub struct Config {
174174
pub indent_guides: IndentGuidesConfig,
175175
/// Whether to color modes with different colors. Defaults to `false`.
176176
pub color_modes: bool,
177+
/// Maximum file size to trigger syntax highlighting
178+
pub syntax_max_file_size: usize,
177179
}
178180

179181
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
@@ -615,6 +617,7 @@ impl Default for Config {
615617
bufferline: BufferLine::default(),
616618
indent_guides: IndentGuidesConfig::default(),
617619
color_modes: false,
620+
syntax_max_file_size: 150000000,
618621
}
619622
}
620623
}
@@ -1098,8 +1101,12 @@ impl Editor {
10981101
let id = if let Some(id) = id {
10991102
id
11001103
} else {
1101-
let mut doc = Document::open(&path, None, Some(self.syn_loader.clone()))?;
1102-
1104+
let mut doc = Document::open(
1105+
&path,
1106+
None,
1107+
Some(self.syn_loader.clone()),
1108+
self.config().syntax_max_file_size,
1109+
)?;
11031110
let _ = Self::launch_language_server(&mut self.language_servers, &mut doc);
11041111

11051112
self.new_document(doc)

0 commit comments

Comments
 (0)