| 1 |
use crate::{ |
| 2 |
annotations::diagnostics::{DiagnosticFilter, InlineDiagnosticsConfig}, |
| 3 |
clipboard::ClipboardProvider, |
| 4 |
document::{ |
| 5 |
DocumentOpenError, DocumentSavedEventFuture, DocumentSavedEventResult, Mode, SavePoint, |
| 6 |
}, |
| 7 |
events::{DocumentDidClose, DocumentDidOpen, DocumentFocusLost}, |
| 8 |
graphics::{CursorKind, Rect}, |
| 9 |
handlers::Handlers, |
| 10 |
info::Info, |
| 11 |
input::KeyEvent, |
| 12 |
register::Registers, |
| 13 |
theme::{self, Theme}, |
| 14 |
tree::{self, Tree}, |
| 15 |
Document, DocumentId, View, ViewId, |
| 16 |
}; |
| 17 |
use helix_event::dispatch; |
| 18 |
use helix_loader::workspace_trust::{ImplicitTrustLevel, TrustQuery, WorkspaceTrust}; |
| 19 |
use helix_vcs::DiffProviderRegistry; |
| 20 |
|
| 21 |
use futures_util::stream::select_all::SelectAll; |
| 22 |
use futures_util::StreamExt; |
| 23 |
use helix_lsp::{Call, LanguageServerId}; |
| 24 |
use tokio_stream::wrappers::UnboundedReceiverStream; |
| 25 |
|
| 26 |
use std::{ |
| 27 |
borrow::Cow, |
| 28 |
cell::Cell, |
| 29 |
collections::{BTreeMap, HashMap, HashSet, VecDeque}, |
| 30 |
fs, |
| 31 |
io::{self, stdin}, |
| 32 |
num::{NonZeroU8, NonZeroUsize}, |
| 33 |
path::{Path, PathBuf}, |
| 34 |
pin::Pin, |
| 35 |
sync::Arc, |
| 36 |
}; |
| 37 |
|
| 38 |
use tokio::{ |
| 39 |
sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}, |
| 40 |
time::{sleep, Duration, Instant, Sleep}, |
| 41 |
}; |
| 42 |
|
| 43 |
use anyhow::{anyhow, bail, Error}; |
| 44 |
|
| 45 |
pub use helix_core::diagnostic::Severity; |
| 46 |
use helix_core::{ |
| 47 |
auto_pairs::AutoPairs, |
| 48 |
diagnostic::DiagnosticProvider, |
| 49 |
syntax::{ |
| 50 |
self, |
| 51 |
config::{AutoPairConfig, IndentationHeuristic, LanguageServerFeature, SoftWrap}, |
| 52 |
}, |
| 53 |
Change, LineEnding, Position, Range, Selection, Uri, NATIVE_LINE_ENDING, |
| 54 |
}; |
| 55 |
use helix_dap::{self as dap, registry::DebugAdapterId}; |
| 56 |
use helix_lsp::lsp; |
| 57 |
use helix_stdx::path::canonicalize; |
| 58 |
|
| 59 |
use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer}; |
| 60 |
|
| 61 |
use arc_swap::{ |
| 62 |
access::{DynAccess, DynGuard}, |
| 63 |
ArcSwap, |
| 64 |
}; |
| 65 |
|
| 66 |
pub const DIR_STACK_CAP: usize = 10; |
| 67 |
pub const DEFAULT_AUTO_SAVE_DELAY: u64 = 3000; |
| 68 |
|
| 69 |
fn deserialize_duration_millis<'de, D>(deserializer: D) -> Result<Duration, D::Error> |
| 70 |
where |
| 71 |
D: serde::Deserializer<'de>, |
| 72 |
{ |
| 73 |
let millis = u64::deserialize(deserializer)?; |
| 74 |
Ok(Duration::from_millis(millis)) |
| 75 |
} |
| 76 |
|
| 77 |
fn serialize_duration_millis<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error> |
| 78 |
where |
| 79 |
S: Serializer, |
| 80 |
{ |
| 81 |
serializer.serialize_u64( |
| 82 |
duration |
| 83 |
.as_millis() |
| 84 |
.try_into() |
| 85 |
.map_err(|_| serde::ser::Error::custom("duration value overflowed u64"))?, |
| 86 |
) |
| 87 |
} |
| 88 |
|
| 89 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 90 |
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)] |
| 91 |
pub struct GutterConfig { |
| 92 |
|
| 93 |
pub layout: Vec<GutterType>, |
| 94 |
|
| 95 |
pub line_numbers: GutterLineNumbersConfig, |
| 96 |
} |
| 97 |
|
| 98 |
impl Default for GutterConfig { |
| 99 |
fn default() -> Self { |
| 100 |
Self { |
| 101 |
layout: vec![ |
| 102 |
GutterType::Diagnostics, |
| 103 |
GutterType::Spacer, |
| 104 |
GutterType::LineNumbers, |
| 105 |
GutterType::Spacer, |
| 106 |
GutterType::Diff, |
| 107 |
], |
| 108 |
line_numbers: GutterLineNumbersConfig::default(), |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
impl From<Vec<GutterType>> for GutterConfig { |
| 114 |
fn from(x: Vec<GutterType>) -> Self { |
| 115 |
GutterConfig { |
| 116 |
layout: x, |
| 117 |
..Default::default() |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
fn deserialize_gutter_seq_or_struct<'de, D>(deserializer: D) -> Result<GutterConfig, D::Error> |
| 123 |
where |
| 124 |
D: Deserializer<'de>, |
| 125 |
{ |
| 126 |
struct GutterVisitor; |
| 127 |
|
| 128 |
impl<'de> serde::de::Visitor<'de> for GutterVisitor { |
| 129 |
type Value = GutterConfig; |
| 130 |
|
| 131 |
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 132 |
write!( |
| 133 |
formatter, |
| 134 |
"an array of gutter names or a detailed gutter configuration" |
| 135 |
) |
| 136 |
} |
| 137 |
|
| 138 |
fn visit_seq<S>(self, mut seq: S) -> Result<Self::Value, S::Error> |
| 139 |
where |
| 140 |
S: serde::de::SeqAccess<'de>, |
| 141 |
{ |
| 142 |
let mut gutters = Vec::new(); |
| 143 |
while let Some(gutter) = seq.next_element::<String>()? { |
| 144 |
gutters.push( |
| 145 |
gutter |
| 146 |
.parse::<GutterType>() |
| 147 |
.map_err(serde::de::Error::custom)?, |
| 148 |
) |
| 149 |
} |
| 150 |
|
| 151 |
Ok(gutters.into()) |
| 152 |
} |
| 153 |
|
| 154 |
fn visit_map<M>(self, map: M) -> Result<Self::Value, M::Error> |
| 155 |
where |
| 156 |
M: serde::de::MapAccess<'de>, |
| 157 |
{ |
| 158 |
let deserializer = serde::de::value::MapAccessDeserializer::new(map); |
| 159 |
Deserialize::deserialize(deserializer) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
deserializer.deserialize_any(GutterVisitor) |
| 164 |
} |
| 165 |
|
| 166 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 167 |
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)] |
| 168 |
pub struct GutterLineNumbersConfig { |
| 169 |
|
| 170 |
pub min_width: usize, |
| 171 |
} |
| 172 |
|
| 173 |
impl Default for GutterLineNumbersConfig { |
| 174 |
fn default() -> Self { |
| 175 |
Self { min_width: 3 } |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 180 |
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)] |
| 181 |
pub struct FilePickerConfig { |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
pub hidden: bool, |
| 186 |
|
| 187 |
|
| 188 |
pub follow_symlinks: bool, |
| 189 |
|
| 190 |
pub deduplicate_links: bool, |
| 191 |
|
| 192 |
pub parents: bool, |
| 193 |
|
| 194 |
|
| 195 |
pub ignore: bool, |
| 196 |
|
| 197 |
|
| 198 |
pub git_ignore: bool, |
| 199 |
|
| 200 |
|
| 201 |
pub git_global: bool, |
| 202 |
|
| 203 |
|
| 204 |
pub git_exclude: bool, |
| 205 |
|
| 206 |
|
| 207 |
pub max_depth: Option<usize>, |
| 208 |
} |
| 209 |
|
| 210 |
impl Default for FilePickerConfig { |
| 211 |
fn default() -> Self { |
| 212 |
Self { |
| 213 |
hidden: true, |
| 214 |
follow_symlinks: true, |
| 215 |
deduplicate_links: true, |
| 216 |
parents: true, |
| 217 |
ignore: true, |
| 218 |
git_ignore: true, |
| 219 |
git_global: true, |
| 220 |
git_exclude: true, |
| 221 |
max_depth: None, |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 227 |
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)] |
| 228 |
pub struct FileExplorerConfig { |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
pub hidden: bool, |
| 233 |
|
| 234 |
|
| 235 |
pub follow_symlinks: bool, |
| 236 |
|
| 237 |
pub parents: bool, |
| 238 |
|
| 239 |
|
| 240 |
pub ignore: bool, |
| 241 |
|
| 242 |
|
| 243 |
pub git_ignore: bool, |
| 244 |
|
| 245 |
|
| 246 |
pub git_global: bool, |
| 247 |
|
| 248 |
|
| 249 |
pub git_exclude: bool, |
| 250 |
|
| 251 |
pub flatten_dirs: bool, |
| 252 |
} |
| 253 |
|
| 254 |
impl Default for FileExplorerConfig { |
| 255 |
fn default() -> Self { |
| 256 |
Self { |
| 257 |
hidden: false, |
| 258 |
follow_symlinks: false, |
| 259 |
parents: false, |
| 260 |
ignore: false, |
| 261 |
git_ignore: false, |
| 262 |
git_global: false, |
| 263 |
git_exclude: false, |
| 264 |
flatten_dirs: true, |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
fn serialize_alphabet<S>(alphabet: &[char], serializer: S) -> Result<S::Ok, S::Error> |
| 270 |
where |
| 271 |
S: Serializer, |
| 272 |
{ |
| 273 |
let alphabet: String = alphabet.iter().collect(); |
| 274 |
serializer.serialize_str(&alphabet) |
| 275 |
} |
| 276 |
|
| 277 |
fn deserialize_alphabet<'de, D>(deserializer: D) -> Result<Vec<char>, D::Error> |
| 278 |
where |
| 279 |
D: Deserializer<'de>, |
| 280 |
{ |
| 281 |
use serde::de::Error; |
| 282 |
|
| 283 |
let str = String::deserialize(deserializer)?; |
| 284 |
let chars: Vec<_> = str.chars().collect(); |
| 285 |
let unique_chars: HashSet<_> = chars.iter().copied().collect(); |
| 286 |
if unique_chars.len() != chars.len() { |
| 287 |
return Err(<D::Error as Error>::custom( |
| 288 |
"jump-label-alphabet must contain unique characters", |
| 289 |
)); |
| 290 |
} |
| 291 |
Ok(chars) |
| 292 |
} |
| 293 |
|
| 294 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 295 |
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)] |
| 296 |
pub struct Config { |
| 297 |
|
| 298 |
pub scrolloff: usize, |
| 299 |
|
| 300 |
pub scroll_lines: isize, |
| 301 |
|
| 302 |
pub mouse: bool, |
| 303 |
|
| 304 |
pub mouse_yank_register: char, |
| 305 |
|
| 306 |
pub shell: Vec<String>, |
| 307 |
|
| 308 |
pub line_number: LineNumber, |
| 309 |
|
| 310 |
pub cursorline: bool, |
| 311 |
|
| 312 |
pub cursorcolumn: bool, |
| 313 |
#[serde(deserialize_with = "deserialize_gutter_seq_or_struct")] |
| 314 |
pub gutters: GutterConfig, |
| 315 |
|
| 316 |
pub middle_click_paste: bool, |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
pub auto_pairs: AutoPairConfig, |
| 321 |
|
| 322 |
pub auto_completion: bool, |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
pub path_completion: bool, |
| 328 |
|
| 329 |
|
| 330 |
pub word_completion: WordCompletion, |
| 331 |
|
| 332 |
pub auto_format: bool, |
| 333 |
|
| 334 |
pub default_yank_register: char, |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
#[serde(deserialize_with = "deserialize_auto_save")] |
| 339 |
pub auto_save: AutoSave, |
| 340 |
|
| 341 |
pub text_width: usize, |
| 342 |
|
| 343 |
|
| 344 |
#[serde( |
| 345 |
serialize_with = "serialize_duration_millis", |
| 346 |
deserialize_with = "deserialize_duration_millis" |
| 347 |
)] |
| 348 |
pub idle_timeout: Duration, |
| 349 |
|
| 350 |
|
| 351 |
#[serde( |
| 352 |
serialize_with = "serialize_duration_millis", |
| 353 |
deserialize_with = "deserialize_duration_millis" |
| 354 |
)] |
| 355 |
pub completion_timeout: Duration, |
| 356 |
|
| 357 |
pub preview_completion_insert: bool, |
| 358 |
pub completion_trigger_len: u8, |
| 359 |
|
| 360 |
|
| 361 |
pub completion_replace: bool, |
| 362 |
|
| 363 |
|
| 364 |
pub continue_comments: bool, |
| 365 |
|
| 366 |
pub auto_info: bool, |
| 367 |
pub file_picker: FilePickerConfig, |
| 368 |
pub file_explorer: FileExplorerConfig, |
| 369 |
|
| 370 |
pub statusline: StatusLineConfig, |
| 371 |
|
| 372 |
pub cursor_shape: CursorShapeConfig, |
| 373 |
|
| 374 |
pub true_color: bool, |
| 375 |
|
| 376 |
pub undercurl: bool, |
| 377 |
|
| 378 |
#[serde(default)] |
| 379 |
pub search: SearchConfig, |
| 380 |
pub lsp: LspConfig, |
| 381 |
pub terminal: Option<TerminalConfig>, |
| 382 |
|
| 383 |
pub rulers: Vec<u16>, |
| 384 |
#[serde(default)] |
| 385 |
pub whitespace: WhitespaceConfig, |
| 386 |
|
| 387 |
pub bufferline: BufferLine, |
| 388 |
|
| 389 |
pub indent_guides: IndentGuidesConfig, |
| 390 |
|
| 391 |
pub color_modes: bool, |
| 392 |
pub soft_wrap: SoftWrap, |
| 393 |
|
| 394 |
pub workspace_lsp_roots: Vec<PathBuf>, |
| 395 |
|
| 396 |
pub default_line_ending: LineEndingConfig, |
| 397 |
|
| 398 |
pub insert_final_newline: bool, |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
pub atomic_save: bool, |
| 403 |
|
| 404 |
|
| 405 |
pub trim_final_newlines: bool, |
| 406 |
|
| 407 |
|
| 408 |
pub trim_trailing_whitespace: bool, |
| 409 |
|
| 410 |
pub smart_tab: Option<SmartTabConfig>, |
| 411 |
|
| 412 |
pub popup_border: PopupBorderConfig, |
| 413 |
|
| 414 |
#[serde(default)] |
| 415 |
pub indent_heuristic: IndentationHeuristic, |
| 416 |
|
| 417 |
#[serde( |
| 418 |
serialize_with = "serialize_alphabet", |
| 419 |
deserialize_with = "deserialize_alphabet" |
| 420 |
)] |
| 421 |
pub jump_label_alphabet: Vec<char>, |
| 422 |
|
| 423 |
pub inline_diagnostics: InlineDiagnosticsConfig, |
| 424 |
pub end_of_line_diagnostics: DiagnosticFilter, |
| 425 |
|
| 426 |
pub clipboard_provider: ClipboardProvider, |
| 427 |
|
| 428 |
|
| 429 |
pub editor_config: bool, |
| 430 |
|
| 431 |
pub rainbow_brackets: bool, |
| 432 |
|
| 433 |
pub kitty_keyboard_protocol: KittyKeyboardProtocolConfig, |
| 434 |
pub buffer_picker: BufferPickerConfig, |
| 435 |
|
| 436 |
pub workspace_trust: WorkspaceTrustConfig, |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)] |
| 441 |
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)] |
| 442 |
pub struct WorkspaceTrustConfig { |
| 443 |
|
| 444 |
pub level: ImplicitTrustLevelConfig, |
| 445 |
|
| 446 |
ld |
| 447 |
|
| 448 |
pub prompt: bool, |
| 449 |
|
| 450 |
pub trusted: Vec<String>, |
| 451 |
} |
| 452 |
|
| 453 |
impl Default for WorkspaceTrustConfig { |
| 454 |
fn default() -> Self { |
| 455 |
Self { |
| 456 |
level: ImplicitTrustLevelConfig::default(), |
| 457 |
prompt: true, |
| 458 |
trusted: Vec::new(), |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize, Clone, Copy)] |
| 464 |
#[serde(rename_all = "kebab-case")] |
| 465 |
pub enum ImplicitTrustLevelConfig { |
| 466 |
e. |
| 467 |
None, |
| 468 |
|
| 469 |
ge |
| 470 |
|
| 471 |
|
| 472 |
#[default] |
| 473 |
Servers, |
| 474 |
|
| 475 |
Insecure, |
| 476 |
} |
| 477 |
|
| 478 |
impl From<ImplicitTrustLevelConfig> for ImplicitTrustLevel { |
| 479 |
fn from(v: ImplicitTrustLevelConfig) -> Self { |
| 480 |
match v { |
| 481 |
ImplicitTrustLevelConfig::None => ImplicitTrustLevel::None, |
| 482 |
ImplicitTrustLevelConfig::Servers => ImplicitTrustLevel::Servers, |
| 483 |
ImplicitTrustLevelConfig::Insecure => ImplicitTrustLevel::Insecure, |
| 484 |
} |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
impl From<&WorkspaceTrustConfig> for helix_loader::workspace_trust::Config { |
| 489 |
fn from(v: &WorkspaceTrustConfig) -> Self { |
| 490 |
Self { |
| 491 |
level: v.level.into(), |
| 492 |
prompt: v.prompt, |
| 493 |
trusted_globs: helix_loader::workspace_trust::build_trusted_globs(&v.trusted), |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
impl Config { |
| 499 |
pub fn code_action_hint(&self) -> bool { |
| 500 |
self.gutters.layout.contains(&GutterType::CodeActionHint) |
| 501 |
|| self |
| 502 |
.statusline |
| 503 |
.left |
| 504 |
.contains(&StatusLineElement::CodeActionHint) |
| 505 |
|| self |
| 506 |
.statusline |
| 507 |
.center |
| 508 |
.contains(&StatusLineElement::CodeActionHint) |
| 509 |
|| self |
| 510 |
.statusline |
| 511 |
.right |
| 512 |
.contains(&StatusLineElement::CodeActionHint) |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize, Clone, Copy)] |
| 517 |
#[serde(rename_all = "kebab-case")] |
| 518 |
pub struct BufferPickerConfig { |
| 519 |
pub start_position: PickerStartPosition, |
| 520 |
} |
| 521 |
|
| 522 |
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize, Clone, Copy)] |
| 523 |
#[serde(rename_all = "kebab-case")] |
| 524 |
pub enum PickerStartPosition { |
| 525 |
#[default] |
| 526 |
Current, |
| 527 |
Previous, |
| 528 |
} |
| 529 |
|
| 530 |
impl PickerStartPosition { |
| 531 |
#[must_use] |
| 532 |
pub fn is_previous(self) -> bool { |
| 533 |
matches!(self, Self::Previous) |
| 534 |
} |
| 535 |
|
| 536 |
#[must_use] |
| 537 |
pub fn is_current(self) -> bool { |
| 538 |
matches!(self, Self::Current) |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize, Clone, Copy)] |
| 543 |
#[serde(rename_all = "kebab-case")] |
| 544 |
pub enum KittyKeyboardProtocolConfig { |
| 545 |
#[default] |
| 546 |
Auto, |
| 547 |
Disabled, |
| 548 |
Enabled, |
| 549 |
} |
| 550 |
|
| 551 |
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq, PartialOrd, Ord)] |
| 552 |
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)] |
| 553 |
pub struct SmartTabConfig { |
| 554 |
pub enable: bool, |
| 555 |
pub supersede_menu: bool, |
| 556 |
} |
| 557 |
|
| 558 |
impl Default for SmartTabConfig { |
| 559 |
fn default() -> Self { |
| 560 |
SmartTabConfig { |
| 561 |
enable: true, |
| 562 |
supersede_menu: false, |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 568 |
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)] |
| 569 |
pub struct TerminalConfig { |
| 570 |
pub command: String, |
| 571 |
#[serde(default)] |
| 572 |
#[serde(skip_serializing_if = "Vec::is_empty")] |
| 573 |
pub args: Vec<String>, |
| 574 |
} |
| 575 |
|
| 576 |
#[cfg(windows)] |
| 577 |
pub fn get_terminal_provider() -> Option<TerminalConfig> { |
| 578 |
use helix_stdx::env::binary_exists; |
| 579 |
|
| 580 |
if binary_exists("wt") { |
| 581 |
return Some(TerminalConfig { |
| 582 |
command: "wt".to_string(), |
| 583 |
args: vec![ |
| 584 |
"new-tab".to_string(), |
| 585 |
"--title".to_string(), |
| 586 |
"DEBUG".to_string(), |
| 587 |
"cmd".to_string(), |
| 588 |
"/C".to_string(), |
| 589 |
], |
| 590 |
}); |
| 591 |
} |
| 592 |
|
| 593 |
Some(TerminalConfig { |
| 594 |
command: "conhost".to_string(), |
| 595 |
args: vec!["cmd".to_string(), "/C".to_string()], |
| 596 |
}) |
| 597 |
} |
| 598 |
|
| 599 |
#[cfg(not(any(windows, target_arch = "wasm32")))] |
| 600 |
pub fn get_terminal_provider() -> Option<TerminalConfig> { |
| 601 |
use helix_stdx::env::{binary_exists, env_var_is_set}; |
| 602 |
|
| 603 |
if env_var_is_set("TMUX") && binary_exists("tmux") { |
| 604 |
return Some(TerminalConfig { |
| 605 |
command: "tmux".to_string(), |
| 606 |
args: vec!["split-window".to_string()], |
| 607 |
}); |
| 608 |
} |
| 609 |
|
| 610 |
if env_var_is_set("WEZTERM_UNIX_SOCKET") && binary_exists("wezterm") { |
| 611 |
return Some(TerminalConfig { |
| 612 |
command: "wezterm".to_string(), |
| 613 |
args: vec!["cli".to_string(), "split-pane".to_string()], |
| 614 |
}); |
| 615 |
} |
| 616 |
|
| 617 |
None |
| 618 |
} |
| 619 |
|
| 620 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 621 |
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)] |
| 622 |
pub struct LspConfig { |
| 623 |
|
| 624 |
pub enable: bool, |
| 625 |
|
| 626 |
pub display_progress_messages: bool, |
| 627 |
|
| 628 |
pub display_messages: bool, |
| 629 |
|
| 630 |
pub auto_signature_help: bool, |
| 631 |
|
| 632 |
pub display_signature_help_docs: bool, |
| 633 |
|
| 634 |
pub display_inlay_hints: bool, |
| 635 |
|
| 636 |
pub auto_document_highlight: bool, |
| 637 |
). |
| 638 |
|
| 639 |
pub inlay_hints_length_limit: Option<NonZeroU8>, |
| 640 |
|
| 641 |
pub display_color_swatches: bool, |
| 642 |
|
| 643 |
pub snippets: bool, |
| 644 |
|
| 645 |
pub goto_reference_include_declaration: bool, |
| 646 |
} |
| 647 |
|
| 648 |
impl Default for LspConfig { |
| 649 |
fn default() -> Self { |
| 650 |
Self { |
| 651 |
enable: true, |
| 652 |
display_progress_messages: false, |
| 653 |
display_messages: true, |
| 654 |
auto_signature_help: true, |
| 655 |
display_signature_help_docs: true, |
| 656 |
display_inlay_hints: false, |
| 657 |
auto_document_highlight: false, |
| 658 |
inlay_hints_length_limit: None, |
| 659 |
snippets: true, |
| 660 |
goto_reference_include_declaration: true, |
| 661 |
display_color_swatches: true, |
| 662 |
} |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 667 |
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)] |
| 668 |
pub struct SearchConfig { |
| 669 |
|
| 670 |
pub smart_case: bool, |
| 671 |
|
| 672 |
pub wrap_around: bool, |
| 673 |
} |
| 674 |
|
| 675 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 676 |
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)] |
| 677 |
pub struct StatusLineConfig { |
| 678 |
pub left: Vec<StatusLineElement>, |
| 679 |
pub center: Vec<StatusLineElement>, |
| 680 |
pub right: Vec<StatusLineElement>, |
| 681 |
pub separator: String, |
| 682 |
pub mode: ModeConfig, |
| 683 |
pub diagnostics: Vec<Severity>, |
| 684 |
pub workspace_diagnostics: Vec<Severity>, |
| 685 |
} |
| 686 |
|
| 687 |
impl Default for StatusLineConfig { |
| 688 |
fn default() -> Self { |
| 689 |
use StatusLineElement as E; |
| 690 |
|
| 691 |
Self { |
| 692 |
left: vec![ |
| 693 |
E::Mode, |
| 694 |
E::Spinner, |
| 695 |
E::FileName, |
| 696 |
E::ReadOnlyIndicator, |
| 697 |
E::FileModificationIndicator, |
| 698 |
], |
| 699 |
center: vec![], |
| 700 |
right: vec![ |
| 701 |
E::Diagnostics, |
| 702 |
E::Selections, |
| 703 |
E::Register, |
| 704 |
E::Position, |
| 705 |
E::FileEncoding, |
| 706 |
], |
| 707 |
separator: String::from("│"), |
| 708 |
mode: ModeConfig::default(), |
| 709 |
diagnostics: vec![Severity::Warning, Severity::Error], |
| 710 |
workspace_diagnostics: vec![Severity::Warning, Severity::Error], |
| 711 |
} |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 716 |
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)] |
| 717 |
pub struct ModeConfig { |
| 718 |
pub normal: String, |
| 719 |
pub insert: String, |
| 720 |
pub select: String, |
| 721 |
} |
| 722 |
|
| 723 |
impl Default for ModeConfig { |
| 724 |
fn default() -> Self { |
| 725 |
Self { |
| 726 |
normal: String::from("NOR"), |
| 727 |
insert: String::from("INS"), |
| 728 |
select: String::from("SEL"), |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 734 |
#[serde(rename_all = "kebab-case")] |
| 735 |
pub enum StatusLineElement { |
| 736 |
|
| 737 |
Mode, |
| 738 |
|
| 739 |
|
| 740 |
Spinner, |
| 741 |
|
| 742 |
|
| 743 |
FileBaseName, |
| 744 |
|
| 745 |
|
| 746 |
FileName, |
| 747 |
|
| 748 |
|
| 749 |
FileAbsolutePath, |
| 750 |
|
| 751 |
|
| 752 |
FileModificationIndicator, |
| 753 |
|
| 754 |
|
| 755 |
ReadOnlyIndicator, |
| 756 |
|
| 757 |
|
| 758 |
FileEncoding, |
| 759 |
|
| 760 |
|
| 761 |
FileLineEnding, |
| 762 |
|
| 763 |
|
| 764 |
FileIndentStyle, |
| 765 |
|
| 766 |
|
| 767 |
FileType, |
| 768 |
|
| 769 |
|
| 770 |
Diagnostics, |
| 771 |
|
| 772 |
|
| 773 |
WorkspaceDiagnostics, |
| 774 |
|
| 775 |
|
| 776 |
Selections, |
| 777 |
|
| 778 |
|
| 779 |
PrimarySelectionLength, |
| 780 |
|
| 781 |
|
| 782 |
Position, |
| 783 |
|
| 784 |
|
| 785 |
Separator, |
| 786 |
|
| 787 |
|
| 788 |
PositionPercentage, |
| 789 |
|
| 790 |
|
| 791 |
TotalLineNumbers, |
| 792 |
|
| 793 |
|
| 794 |
Spacer, |
| 795 |
|
| 796 |
|
| 797 |
VersionControl, |
| 798 |
|
| 799 |
|
| 800 |
Register, |
| 801 |
|
| 802 |
|
| 803 |
CurrentWorkingDirectory, |
| 804 |
|
| 805 |
|
| 806 |
CodeActionHint, |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 812 |
pub struct CursorShapeConfig([CursorKind; 3]); |
| 813 |
|
| 814 |
impl CursorShapeConfig { |
| 815 |
pub fn from_mode(&self, mode: Mode) -> CursorKind { |
| 816 |
self.get(mode as usize).copied().unwrap_or_default() |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
impl<'de> Deserialize<'de> for CursorShapeConfig { |
| 821 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 822 |
where |
| 823 |
D: Deserializer<'de>, |
| 824 |
{ |
| 825 |
let m = HashMap::<Mode, CursorKind>::deserialize(deserializer)?; |
| 826 |
let into_cursor = |mode: Mode| m.get(&mode).copied().unwrap_or_default(); |
| 827 |
Ok(CursorShapeConfig([ |
| 828 |
into_cursor(Mode::Normal), |
| 829 |
into_cursor(Mode::Select), |
| 830 |
into_cursor(Mode::Insert), |
| 831 |
])) |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
impl Serialize for CursorShapeConfig { |
| 836 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 837 |
where |
| 838 |
S: serde::Serializer, |
| 839 |
{ |
| 840 |
let mut map = serializer.serialize_map(Some(self.len()))?; |
| 841 |
let modes = [Mode::Normal, Mode::Select, Mode::Insert]; |
| 842 |
for mode in modes { |
| 843 |
map.serialize_entry(&mode, &self.from_mode(mode))?; |
| 844 |
} |
| 845 |
map.end() |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
impl std::ops::Deref for CursorShapeConfig { |
| 850 |
type Target = [CursorKind; 3]; |
| 851 |
|
| 852 |
fn deref(&self) -> &Self::Target { |
| 853 |
&self.0 |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
impl Default for CursorShapeConfig { |
| 858 |
fn default() -> Self { |
| 859 |
Self([CursorKind::Block; 3]) |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 865 |
#[serde(rename_all = "kebab-case")] |
| 866 |
pub enum BufferLine { |
| 867 |
|
| 868 |
#[default] |
| 869 |
Never, |
| 870 |
|
| 871 |
Always, |
| 872 |
|
| 873 |
Multiple, |
| 874 |
} |
| 875 |
|
| 876 |
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 877 |
#[serde(rename_all = "kebab-case")] |
| 878 |
pub enum LineNumber { |
| 879 |
|
| 880 |
Absolute, |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
Relative, |
| 885 |
} |
| 886 |
|
| 887 |
impl std::str::FromStr for LineNumber { |
| 888 |
type Err = anyhow::Error; |
| 889 |
|
| 890 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 891 |
match s.to_lowercase().as_str() { |
| 892 |
"absolute" | "abs" => Ok(Self::Absolute), |
| 893 |
"relative" | "rel" => Ok(Self::Relative), |
| 894 |
_ => anyhow::bail!("Line number can only be `absolute` or `relative`."), |
| 895 |
} |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 900 |
#[serde(rename_all = "kebab-case")] |
| 901 |
pub enum GutterType { |
| 902 |
|
| 903 |
Diagnostics, |
| 904 |
|
| 905 |
LineNumbers, |
| 906 |
|
| 907 |
Spacer, |
| 908 |
|
| 909 |
Diff, |
| 910 |
|
| 911 |
CodeActionHint, |
| 912 |
} |
| 913 |
|
| 914 |
impl std::str::FromStr for GutterType { |
| 915 |
type Err = anyhow::Error; |
| 916 |
|
| 917 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 918 |
match s.to_lowercase().as_str() { |
| 919 |
"diagnostics" => Ok(Self::Diagnostics), |
| 920 |
"spacer" => Ok(Self::Spacer), |
| 921 |
"line-numbers" => Ok(Self::LineNumbers), |
| 922 |
"diff" => Ok(Self::Diff), |
| 923 |
"code-action-hint" => Ok(Self::CodeActionHint), |
| 924 |
_ => anyhow::bail!( |
| 925 |
"Gutter type can only be `diagnostics`, `spacer`, `line-numbers` or `diff`." |
| 926 |
), |
| 927 |
} |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 932 |
#[serde(default)] |
| 933 |
pub struct WhitespaceConfig { |
| 934 |
pub render: WhitespaceRender, |
| 935 |
pub characters: WhitespaceCharacters, |
| 936 |
} |
| 937 |
|
| 938 |
impl Default for WhitespaceConfig { |
| 939 |
fn default() -> Self { |
| 940 |
Self { |
| 941 |
render: WhitespaceRender::Basic(WhitespaceRenderValue::None), |
| 942 |
characters: WhitespaceCharacters::default(), |
| 943 |
} |
| 944 |
} |
| 945 |
} |
| 946 |
|
| 947 |
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 948 |
#[serde(untagged, rename_all = "kebab-case")] |
| 949 |
pub enum WhitespaceRender { |
| 950 |
Basic(WhitespaceRenderValue), |
| 951 |
Specific { |
| 952 |
default: Option<WhitespaceRenderValue>, |
| 953 |
space: Option<WhitespaceRenderValue>, |
| 954 |
nbsp: Option<WhitespaceRenderValue>, |
| 955 |
nnbsp: Option<WhitespaceRenderValue>, |
| 956 |
tab: Option<WhitespaceRenderValue>, |
| 957 |
newline: Option<WhitespaceRenderValue>, |
| 958 |
}, |
| 959 |
} |
| 960 |
|
| 961 |
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 962 |
#[serde(rename_all = "kebab-case")] |
| 963 |
pub enum WhitespaceRenderValue { |
| 964 |
None, |
| 965 |
|
| 966 |
|
| 967 |
All, |
| 968 |
} |
| 969 |
|
| 970 |
impl WhitespaceRender { |
| 971 |
pub fn space(&self) -> WhitespaceRenderValue { |
| 972 |
match *self { |
| 973 |
Self::Basic(val) => val, |
| 974 |
Self::Specific { default, space, .. } => { |
| 975 |
space.or(default).unwrap_or(WhitespaceRenderValue::None) |
| 976 |
} |
| 977 |
} |
| 978 |
} |
| 979 |
pub fn nbsp(&self) -> WhitespaceRenderValue { |
| 980 |
match *self { |
| 981 |
Self::Basic(val) => val, |
| 982 |
Self::Specific { default, nbsp, .. } => { |
| 983 |
nbsp.or(default).unwrap_or(WhitespaceRenderValue::None) |
| 984 |
} |
| 985 |
} |
| 986 |
} |
| 987 |
pub fn nnbsp(&self) -> WhitespaceRenderValue { |
| 988 |
match *self { |
| 989 |
Self::Basic(val) => val, |
| 990 |
Self::Specific { default, nnbsp, .. } => { |
| 991 |
nnbsp.or(default).unwrap_or(WhitespaceRenderValue::None) |
| 992 |
} |
| 993 |
} |
| 994 |
} |
| 995 |
pub fn tab(&self) -> WhitespaceRenderValue { |
| 996 |
match *self { |
| 997 |
Self::Basic(val) => val, |
| 998 |
Self::Specific { default, tab, .. } => { |
| 999 |
tab.or(default).unwrap_or(WhitespaceRenderValue::None) |
| 1000 |
} |
| 1001 |
} |
| 1002 |
} |
| 1003 |
pub fn newline(&self) -> WhitespaceRenderValue { |
| 1004 |
match *self { |
| 1005 |
Self::Basic(val) => val, |
| 1006 |
Self::Specific { |
| 1007 |
default, newline, .. |
| 1008 |
} => newline.or(default).unwrap_or(WhitespaceRenderValue::None), |
| 1009 |
} |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)] |
| 1014 |
#[serde(rename_all = "kebab-case")] |
| 1015 |
pub struct AutoSave { |
| 1016 |
|
| 1017 |
#[serde(default)] |
| 1018 |
pub after_delay: AutoSaveAfterDelay, |
| 1019 |
|
| 1020 |
#[serde(default)] |
| 1021 |
pub focus_lost: bool, |
| 1022 |
} |
| 1023 |
|
| 1024 |
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] |
| 1025 |
#[serde(deny_unknown_fields)] |
| 1026 |
pub struct AutoSaveAfterDelay { |
| 1027 |
#[serde(default)] |
| 1028 |
|
| 1029 |
pub enable: bool, |
| 1030 |
#[serde(default = "default_auto_save_delay")] |
| 1031 |
|
| 1032 |
pub timeout: u64, |
| 1033 |
} |
| 1034 |
|
| 1035 |
impl Default for AutoSaveAfterDelay { |
| 1036 |
fn default() -> Self { |
| 1037 |
Self { |
| 1038 |
enable: false, |
| 1039 |
timeout: DEFAULT_AUTO_SAVE_DELAY, |
| 1040 |
} |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
fn default_auto_save_delay() -> u64 { |
| 1045 |
DEFAULT_AUTO_SAVE_DELAY |
| 1046 |
} |
| 1047 |
|
| 1048 |
fn deserialize_auto_save<'de, D>(deserializer: D) -> Result<AutoSave, D::Error> |
| 1049 |
where |
| 1050 |
D: serde::Deserializer<'de>, |
| 1051 |
{ |
| 1052 |
#[derive(Deserialize, Serialize)] |
| 1053 |
#[serde(untagged, deny_unknown_fields, rename_all = "kebab-case")] |
| 1054 |
enum AutoSaveToml { |
| 1055 |
EnableFocusLost(bool), |
| 1056 |
AutoSave(AutoSave), |
| 1057 |
} |
| 1058 |
|
| 1059 |
match AutoSaveToml::deserialize(deserializer)? { |
| 1060 |
AutoSaveToml::EnableFocusLost(focus_lost) => Ok(AutoSave { |
| 1061 |
focus_lost, |
| 1062 |
..Default::default() |
| 1063 |
}), |
| 1064 |
AutoSaveToml::AutoSave(auto_save) => Ok(auto_save), |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 1069 |
#[serde(default)] |
| 1070 |
pub struct WhitespaceCharacters { |
| 1071 |
pub space: char, |
| 1072 |
pub nbsp: char, |
| 1073 |
pub nnbsp: char, |
| 1074 |
pub tab: char, |
| 1075 |
pub tabpad: char, |
| 1076 |
pub newline: char, |
| 1077 |
} |
| 1078 |
|
| 1079 |
impl Default for WhitespaceCharacters { |
| 1080 |
fn default() -> Self { |
| 1081 |
Self { |
| 1082 |
space: '·', 7 |
| 1083 |
nbsp: '⍽', 7D |
| 1084 |
nnbsp: '␣', 23 |
| 1085 |
tab: '→', 92 |
| 1086 |
newline: '⏎'CE |
| 1087 |
tabpad: ' ', |
| 1088 |
} |
| 1089 |
} |
| 1090 |
} |
| 1091 |
|
| 1092 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 1093 |
#[serde(default, rename_all = "kebab-case")] |
| 1094 |
pub struct IndentGuidesConfig { |
| 1095 |
pub render: bool, |
| 1096 |
pub character: char, |
| 1097 |
pub skip_levels: u8, |
| 1098 |
} |
| 1099 |
|
| 1100 |
impl Default for IndentGuidesConfig { |
| 1101 |
fn default() -> Self { |
| 1102 |
Self { |
| 1103 |
skip_levels: 0, |
| 1104 |
render: false, |
| 1105 |
character: '│', |
| 1106 |
} |
| 1107 |
} |
| 1108 |
} |
| 1109 |
|
| 1110 |
|
| 1111 |
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 1112 |
#[serde(rename_all = "lowercase")] |
| 1113 |
pub enum LineEndingConfig { |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
#[default] |
| 1118 |
Native, |
| 1119 |
|
| 1120 |
LF, |
| 1121 |
|
| 1122 |
Crlf, |
| 1123 |
|
| 1124 |
#[cfg(feature = "unicode-lines")] |
| 1125 |
FF, |
| 1126 |
|
| 1127 |
#[cfg(feature = "unicode-lines")] |
| 1128 |
CR, |
| 1129 |
|
| 1130 |
#[cfg(feature = "unicode-lines")] |
| 1131 |
Nel, |
| 1132 |
} |
| 1133 |
|
| 1134 |
impl From<LineEndingConfig> for LineEnding { |
| 1135 |
fn from(line_ending: LineEndingConfig) -> Self { |
| 1136 |
match line_ending { |
| 1137 |
LineEndingConfig::Native => NATIVE_LINE_ENDING, |
| 1138 |
LineEndingConfig::LF => LineEnding::LF, |
| 1139 |
LineEndingConfig::Crlf => LineEnding::Crlf, |
| 1140 |
#[cfg(feature = "unicode-lines")] |
| 1141 |
LineEndingConfig::FF => LineEnding::FF, |
| 1142 |
#[cfg(feature = "unicode-lines")] |
| 1143 |
LineEndingConfig::CR => LineEnding::CR, |
| 1144 |
#[cfg(feature = "unicode-lines")] |
| 1145 |
LineEndingConfig::Nel => LineEnding::Nel, |
| 1146 |
} |
| 1147 |
} |
| 1148 |
} |
| 1149 |
|
| 1150 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 1151 |
#[serde(rename_all = "kebab-case")] |
| 1152 |
pub enum PopupBorderConfig { |
| 1153 |
None, |
| 1154 |
All, |
| 1155 |
Popup, |
| 1156 |
Menu, |
| 1157 |
} |
| 1158 |
|
| 1159 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 1160 |
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)] |
| 1161 |
pub struct WordCompletion { |
| 1162 |
pub enable: bool, |
| 1163 |
pub trigger_length: NonZeroU8, |
| 1164 |
} |
| 1165 |
|
| 1166 |
impl Default for WordCompletion { |
| 1167 |
fn default() -> Self { |
| 1168 |
Self { |
| 1169 |
enable: true, |
| 1170 |
trigger_length: NonZeroU8::new(7).unwrap(), |
| 1171 |
} |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
impl Default for Config { |
| 1176 |
fn default() -> Self { |
| 1177 |
Self { |
| 1178 |
scrolloff: 5, |
| 1179 |
scroll_lines: 3, |
| 1180 |
mouse: true, |
| 1181 |
mouse_yank_register: '*', |
| 1182 |
shell: if cfg!(windows) { |
| 1183 |
vec!["cmd".to_owned(), "/C".to_owned()] |
| 1184 |
} else { |
| 1185 |
vec!["sh".to_owned(), "-c".to_owned()] |
| 1186 |
}, |
| 1187 |
line_number: LineNumber::Absolute, |
| 1188 |
cursorline: false, |
| 1189 |
cursorcolumn: false, |
| 1190 |
gutters: GutterConfig::default(), |
| 1191 |
middle_click_paste: true, |
| 1192 |
auto_pairs: AutoPairConfig::default(), |
| 1193 |
auto_completion: true, |
| 1194 |
path_completion: true, |
| 1195 |
word_completion: WordCompletion::default(), |
| 1196 |
auto_format: true, |
| 1197 |
default_yank_register: '"', |
| 1198 |
auto_save: AutoSave::default(), |
| 1199 |
idle_timeout: Duration::from_millis(250), |
| 1200 |
completion_timeout: Duration::from_millis(250), |
| 1201 |
preview_completion_insert: true, |
| 1202 |
completion_trigger_len: 2, |
| 1203 |
auto_info: true, |
| 1204 |
file_picker: FilePickerConfig::default(), |
| 1205 |
file_explorer: FileExplorerConfig::default(), |
| 1206 |
statusline: StatusLineConfig::default(), |
| 1207 |
cursor_shape: CursorShapeConfig::default(), |
| 1208 |
true_color: false, |
| 1209 |
undercurl: false, |
| 1210 |
search: SearchConfig::default(), |
| 1211 |
lsp: LspConfig::default(), |
| 1212 |
terminal: get_terminal_provider(), |
| 1213 |
rulers: Vec::new(), |
| 1214 |
whitespace: WhitespaceConfig::default(), |
| 1215 |
bufferline: BufferLine::default(), |
| 1216 |
indent_guides: IndentGuidesConfig::default(), |
| 1217 |
color_modes: false, |
| 1218 |
soft_wrap: SoftWrap { |
| 1219 |
enable: Some(false), |
| 1220 |
..SoftWrap::default() |
| 1221 |
}, |
| 1222 |
text_width: 80, |
| 1223 |
completion_replace: false, |
| 1224 |
continue_comments: true, |
| 1225 |
workspace_lsp_roots: Vec::new(), |
| 1226 |
default_line_ending: LineEndingConfig::default(), |
| 1227 |
insert_final_newline: true, |
| 1228 |
atomic_save: true, |
| 1229 |
trim_final_newlines: false, |
| 1230 |
trim_trailing_whitespace: false, |
| 1231 |
smart_tab: Some(SmartTabConfig::default()), |
| 1232 |
popup_border: PopupBorderConfig::None, |
| 1233 |
indent_heuristic: IndentationHeuristic::default(), |
| 1234 |
jump_label_alphabet: ('a'..='z').collect(), |
| 1235 |
inline_diagnostics: InlineDiagnosticsConfig::default(), |
| 1236 |
end_of_line_diagnostics: DiagnosticFilter::Enable(Severity::Hint), |
| 1237 |
clipboard_provider: ClipboardProvider::default(), |
| 1238 |
editor_config: true, |
| 1239 |
rainbow_brackets: false, |
| 1240 |
kitty_keyboard_protocol: Default::default(), |
| 1241 |
buffer_picker: BufferPickerConfig::default(), |
| 1242 |
workspace_trust: WorkspaceTrustConfig::default(), |
| 1243 |
} |
| 1244 |
} |
| 1245 |
} |
| 1246 |
|
| 1247 |
impl Default for SearchConfig { |
| 1248 |
fn default() -> Self { |
| 1249 |
Self { |
| 1250 |
wrap_around: true, |
| 1251 |
smart_case: true, |
| 1252 |
} |
| 1253 |
} |
| 1254 |
} |
| 1255 |
|
| 1256 |
#[derive(Debug, Clone, Default)] |
| 1257 |
pub struct Breakpoint { |
| 1258 |
pub id: Option<usize>, |
| 1259 |
pub verified: bool, |
| 1260 |
pub message: Option<String>, |
| 1261 |
|
| 1262 |
pub line: usize, |
| 1263 |
pub column: Option<usize>, |
| 1264 |
pub condition: Option<String>, |
| 1265 |
pub hit_condition: Option<String>, |
| 1266 |
pub log_message: Option<String>, |
| 1267 |
} |
| 1268 |
|
| 1269 |
use futures_util::stream::{Flatten, Once}; |
| 1270 |
|
| 1271 |
type Diagnostics = BTreeMap<Uri, Vec<(lsp::Diagnostic, DiagnosticProvider)>>; |
| 1272 |
|
| 1273 |
pub struct Editor { |
| 1274 |
|
| 1275 |
pub mode: Mode, |
| 1276 |
pub tree: Tree, |
| 1277 |
pub next_document_id: DocumentId, |
| 1278 |
pub documents: BTreeMap<DocumentId, Document>, |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
pub saves: HashMap<DocumentId, UnboundedSender<Once<DocumentSavedEventFuture>>>, |
| 1283 |
pub save_queue: SelectAll<Flatten<UnboundedReceiverStream<Once<DocumentSavedEventFuture>>>>, |
| 1284 |
pub write_count: usize, |
| 1285 |
|
| 1286 |
pub count: Option<std::num::NonZeroUsize>, |
| 1287 |
pub selected_register: Option<char>, |
| 1288 |
pub registers: Registers, |
| 1289 |
pub macro_recording: Option<(char, Vec<KeyEvent>)>, |
| 1290 |
pub macro_replaying: Vec<char>, |
| 1291 |
pub language_servers: helix_lsp::Registry, |
| 1292 |
pub diagnostics: Diagnostics, |
| 1293 |
pub diff_providers: DiffProviderRegistry, |
| 1294 |
|
| 1295 |
pub debug_adapters: dap::registry::Registry, |
| 1296 |
pub breakpoints: HashMap<PathBuf, Vec<Breakpoint>>, |
| 1297 |
|
| 1298 |
pub syn_loader: Arc<ArcSwap<syntax::Loader>>, |
| 1299 |
pub theme_loader: Arc<theme::Loader>, |
| 1300 |
|
| 1301 |
|
| 1302 |
pub last_theme: Option<Theme>, |
| 1303 |
|
| 1304 |
|
| 1305 |
pub theme: Theme, |
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
pub last_selection: Option<Selection>, |
| 1311 |
|
| 1312 |
pub status_msg: Option<(Cow<'static, str>, Severity)>, |
| 1313 |
pub autoinfo: Option<Info>, |
| 1314 |
|
| 1315 |
pub config: Arc<dyn DynAccess<Config>>, |
| 1316 |
pub auto_pairs: Option<AutoPairs>, |
| 1317 |
|
| 1318 |
pub idle_timer: Pin<Box<Sleep>>, |
| 1319 |
redraw_timer: Pin<Box<Sleep>>, |
| 1320 |
last_motion: Option<Motion>, |
| 1321 |
pub last_completion: Option<CompleteAction>, |
| 1322 |
pub last_cwd: Option<PathBuf>, |
| 1323 |
pub dir_stack: VecDeque<PathBuf>, |
| 1324 |
|
| 1325 |
pub exit_code: i32, |
| 1326 |
|
| 1327 |
pub config_events: (UnboundedSender<ConfigEvent>, UnboundedReceiver<ConfigEvent>), |
| 1328 |
pub needs_redraw: bool, |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
pub handlers: Handlers, |
| 1342 |
|
| 1343 |
pub mouse_down_range: Option<Range>, |
| 1344 |
pub cursor_cache: CursorCache, |
| 1345 |
pub workspace_trust: WorkspaceTrust, |
| 1346 |
} |
| 1347 |
|
| 1348 |
pub type Motion = Box<dyn Fn(&mut Editor)>; |
| 1349 |
|
| 1350 |
#[derive(Debug)] |
| 1351 |
pub enum EditorEvent { |
| 1352 |
DocumentSaved(DocumentSavedEventResult), |
| 1353 |
ConfigEvent(ConfigEvent), |
| 1354 |
LanguageServerMessage((LanguageServerId, Call)), |
| 1355 |
DebuggerEvent((DebugAdapterId, dap::Payload)), |
| 1356 |
IdleTimer, |
| 1357 |
Redraw, |
| 1358 |
} |
| 1359 |
|
| 1360 |
#[derive(Debug, Clone)] |
| 1361 |
pub enum ConfigEvent { |
| 1362 |
Refresh, |
| 1363 |
Update(Box<Config>), |
| 1364 |
ThemeChanged, |
| 1365 |
} |
| 1366 |
|
| 1367 |
enum ThemeAction { |
| 1368 |
Set, |
| 1369 |
Preview, |
| 1370 |
} |
| 1371 |
|
| 1372 |
#[derive(Debug, Clone)] |
| 1373 |
pub enum CompleteAction { |
| 1374 |
Triggered, |
| 1375 |
|
| 1376 |
|
| 1377 |
Selected { |
| 1378 |
savepoint: Arc<SavePoint>, |
| 1379 |
}, |
| 1380 |
Applied { |
| 1381 |
trigger_offset: usize, |
| 1382 |
changes: Vec<Change>, |
| 1383 |
placeholder: bool, |
| 1384 |
}, |
| 1385 |
} |
| 1386 |
|
| 1387 |
#[derive(Debug, Copy, Clone)] |
| 1388 |
pub enum Action { |
| 1389 |
Load, |
| 1390 |
Replace, |
| 1391 |
HorizontalSplit, |
| 1392 |
VerticalSplit, |
| 1393 |
} |
| 1394 |
|
| 1395 |
impl Action { |
| 1396 |
|
| 1397 |
pub fn align_view(&self, view: &View, new_doc: DocumentId) -> bool { |
| 1398 |
!matches!((self, view.doc == new_doc), (Action::Load, false)) |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
|
| 1403 |
pub enum CloseError { |
| 1404 |
|
| 1405 |
DoesNotExist, |
| 1406 |
|
| 1407 |
BufferModified(String), |
| 1408 |
|
| 1409 |
SaveError(anyhow::Error), |
| 1410 |
} |
| 1411 |
|
| 1412 |
impl Editor { |
| 1413 |
pub fn new( |
| 1414 |
mut area: Rect, |
| 1415 |
theme_loader: Arc<theme::Loader>, |
| 1416 |
syn_loader: Arc<ArcSwap<syntax::Loader>>, |
| 1417 |
config: Arc<dyn DynAccess<Config>>, |
| 1418 |
handlers: Handlers, |
| 1419 |
workspace_trust: WorkspaceTrust, |
| 1420 |
) -> Self { |
| 1421 |
let language_servers = helix_lsp::Registry::new(syn_loader.clone()); |
| 1422 |
let conf = config.load(); |
| 1423 |
let auto_pairs = (&conf.auto_pairs).into(); |
| 1424 |
|
| 1425 |
|
| 1426 |
area.height -= 1; |
| 1427 |
|
| 1428 |
Self { |
| 1429 |
mode: Mode::Normal, |
| 1430 |
tree: Tree::new(area), |
| 1431 |
next_document_id: DocumentId::default(), |
| 1432 |
documents: BTreeMap::new(), |
| 1433 |
saves: HashMap::new(), |
| 1434 |
save_queue: SelectAll::new(), |
| 1435 |
write_count: 0, |
| 1436 |
count: None, |
| 1437 |
selected_register: None, |
| 1438 |
macro_recording: None, |
| 1439 |
macro_replaying: Vec::new(), |
| 1440 |
theme: theme_loader.default(), |
| 1441 |
language_servers, |
| 1442 |
diagnostics: Diagnostics::new(), |
| 1443 |
diff_providers: DiffProviderRegistry::default(), |
| 1444 |
debug_adapters: dap::registry::Registry::new(), |
| 1445 |
breakpoints: HashMap::new(), |
| 1446 |
syn_loader, |
| 1447 |
theme_loader, |
| 1448 |
last_theme: None, |
| 1449 |
last_selection: None, |
| 1450 |
registers: Registers::new(Box::new(arc_swap::access::Map::new( |
| 1451 |
Arc::clone(&config), |
| 1452 |
|config: &Config| &config.clipboard_provider, |
| 1453 |
))), |
| 1454 |
status_msg: None, |
| 1455 |
autoinfo: None, |
| 1456 |
idle_timer: Box::pin(sleep(conf.idle_timeout)), |
| 1457 |
redraw_timer: Box::pin(sleep(Duration::MAX)), |
| 1458 |
last_motion: None, |
| 1459 |
last_completion: None, |
| 1460 |
last_cwd: None, |
| 1461 |
config, |
| 1462 |
auto_pairs, |
| 1463 |
exit_code: 0, |
| 1464 |
config_events: unbounded_channel(), |
| 1465 |
needs_redraw: false, |
| 1466 |
handlers, |
| 1467 |
mouse_down_range: None, |
| 1468 |
cursor_cache: CursorCache::default(), |
| 1469 |
dir_stack: VecDeque::with_capacity(DIR_STACK_CAP), |
| 1470 |
workspace_trust, |
| 1471 |
} |
| 1472 |
} |
| 1473 |
|
| 1474 |
pub fn popup_border(&self) -> bool { |
| 1475 |
self.config().popup_border == PopupBorderConfig::All |
| 1476 |
|| self.config().popup_border == PopupBorderConfig::Popup |
| 1477 |
} |
| 1478 |
|
| 1479 |
pub fn menu_border(&self) -> bool { |
| 1480 |
self.config().popup_border == PopupBorderConfig::All |
| 1481 |
|| self.config().popup_border == PopupBorderConfig::Menu |
| 1482 |
} |
| 1483 |
|
| 1484 |
pub fn apply_motion<F: Fn(&mut Self) + 'static>(&mut self, motion: F) { |
| 1485 |
motion(self); |
| 1486 |
self.last_motion = Some(Box::new(motion)); |
| 1487 |
} |
| 1488 |
|
| 1489 |
pub fn repeat_last_motion(&mut self, count: usize) { |
| 1490 |
if let Some(motion) = self.last_motion.take() { |
| 1491 |
for _ in 0..count { |
| 1492 |
motion(self); |
| 1493 |
} |
| 1494 |
self.last_motion = Some(motion); |
| 1495 |
} |
| 1496 |
} |
| 1497 |
|
| 1498 |
pub fn mode(&self) -> Mode { |
| 1499 |
self.mode |
| 1500 |
} |
| 1501 |
|
| 1502 |
pub fn config(&self) -> DynGuard<Config> { |
| 1503 |
self.config.load() |
| 1504 |
} |
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
pub fn refresh_config(&mut self, old_config: &Config) { |
| 1509 |
let config = self.config(); |
| 1510 |
self.auto_pairs = (&config.auto_pairs).into(); |
| 1511 |
self.reset_idle_timer(); |
| 1512 |
self._refresh(); |
| 1513 |
helix_event::dispatch(crate::events::ConfigDidChange { |
| 1514 |
editor: self, |
| 1515 |
old: old_config, |
| 1516 |
new: &config, |
| 1517 |
}) |
| 1518 |
} |
| 1519 |
|
| 1520 |
pub fn clear_idle_timer(&mut self) { |
| 1521 |
|
| 1522 |
self.idle_timer |
| 1523 |
.as_mut() |
| 1524 |
.reset(Instant::now() + Duration::from_secs(86400 * 365 * 30)); |
| 1525 |
} |
| 1526 |
|
| 1527 |
pub fn reset_idle_timer(&mut self) { |
| 1528 |
let config = self.config(); |
| 1529 |
self.idle_timer |
| 1530 |
.as_mut() |
| 1531 |
.reset(Instant::now() + config.idle_timeout); |
| 1532 |
} |
| 1533 |
|
| 1534 |
pub fn clear_status(&mut self) { |
| 1535 |
self.status_msg = None; |
| 1536 |
} |
| 1537 |
|
| 1538 |
#[inline] |
| 1539 |
pub fn set_status<T: Into<Cow<'static, str>>>(&mut self, status: T) { |
| 1540 |
let status = status.into(); |
| 1541 |
log::debug!("editor status: {}", status); |
| 1542 |
self.status_msg = Some((status, Severity::Info)); |
| 1543 |
} |
| 1544 |
|
| 1545 |
#[inline] |
| 1546 |
pub fn set_error<T: Into<Cow<'static, str>>>(&mut self, error: T) { |
| 1547 |
let error = error.into(); |
| 1548 |
log::debug!("editor error: {}", error); |
| 1549 |
self.status_msg = Some((error, Severity::Error)); |
| 1550 |
} |
| 1551 |
|
| 1552 |
#[inline] |
| 1553 |
pub fn set_warning<T: Into<Cow<'static, str>>>(&mut self, warning: T) { |
| 1554 |
let warning = warning.into(); |
| 1555 |
log::warn!("editor warning: {}", warning); |
| 1556 |
self.status_msg = Some((warning, Severity::Warning)); |
| 1557 |
} |
| 1558 |
|
| 1559 |
#[inline] |
| 1560 |
pub fn get_status(&self) -> Option<(&Cow<'static, str>, &Severity)> { |
| 1561 |
self.status_msg.as_ref().map(|(status, sev)| (status, sev)) |
| 1562 |
} |
| 1563 |
|
| 1564 |
|
| 1565 |
#[inline] |
| 1566 |
pub fn is_err(&self) -> bool { |
| 1567 |
self.status_msg |
| 1568 |
.as_ref() |
| 1569 |
.map(|(_, sev)| *sev == Severity::Error) |
| 1570 |
.unwrap_or(false) |
| 1571 |
} |
| 1572 |
|
| 1573 |
pub fn unset_theme_preview(&mut self) -> anyhow::Result<()> { |
| 1574 |
if let Some(last_theme) = self.last_theme.take() { |
| 1575 |
self.set_theme(last_theme)?; |
| 1576 |
} |
| 1577 |
|
| 1578 |
Ok(()) |
| 1579 |
} |
| 1580 |
|
| 1581 |
pub fn set_theme_preview(&mut self, theme: Theme) -> anyhow::Result<()> { |
| 1582 |
self.set_theme_impl(theme, ThemeAction::Preview) |
| 1583 |
} |
| 1584 |
|
| 1585 |
pub fn set_theme(&mut self, theme: Theme) -> anyhow::Result<()> { |
| 1586 |
self.set_theme_impl(theme, ThemeAction::Set) |
| 1587 |
} |
| 1588 |
|
| 1589 |
fn set_theme_impl(&mut self, theme: Theme, preview: ThemeAction) -> anyhow::Result<()> { |
| 1590 |
|
| 1591 |
if theme.find_highlight_exact("ui.selection").is_none() { |
| 1592 |
bail!("Invalid theme: `ui.selection` required"); |
| 1593 |
} |
| 1594 |
|
| 1595 |
let scopes = theme.scopes(); |
| 1596 |
(*self.syn_loader).load().set_scopes(scopes.to_vec()); |
| 1597 |
|
| 1598 |
match preview { |
| 1599 |
ThemeAction::Preview => { |
| 1600 |
let last_theme = std::mem::replace(&mut self.theme, theme); |
| 1601 |
|
| 1602 |
self.last_theme.get_or_insert(last_theme); |
| 1603 |
} |
| 1604 |
ThemeAction::Set => { |
| 1605 |
self.last_theme = None; |
| 1606 |
self.theme = theme; |
| 1607 |
} |
| 1608 |
} |
| 1609 |
|
| 1610 |
self._refresh(); |
| 1611 |
self.config_events.0.send(ConfigEvent::ThemeChanged)?; |
| 1612 |
|
| 1613 |
Ok(()) |
| 1614 |
} |
| 1615 |
|
| 1616 |
#[inline] |
| 1617 |
pub fn language_server_by_id( |
| 1618 |
&self, |
| 1619 |
language_server_id: LanguageServerId, |
| 1620 |
) -> Option<&helix_lsp::Client> { |
| 1621 |
self.language_servers |
| 1622 |
.get_by_id(language_server_id) |
| 1623 |
.map(|client| &**client) |
| 1624 |
} |
| 1625 |
|
| 1626 |
|
| 1627 |
pub fn refresh_language_servers(&mut self, doc_id: DocumentId) { |
| 1628 |
self.launch_language_servers(doc_id) |
| 1629 |
} |
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
pub fn move_path(&mut self, old_path: &Path, new_path: &Path) -> io::Result<()> { |
| 1634 |
let new_path = canonicalize(new_path); |
| 1635 |
|
| 1636 |
if old_path == new_path { |
| 1637 |
return Ok(()); |
| 1638 |
} |
| 1639 |
let is_dir = old_path.is_dir(); |
| 1640 |
let language_servers: Vec<_> = self |
| 1641 |
.language_servers |
| 1642 |
.iter_clients() |
| 1643 |
.filter(|client| client.is_initialized()) |
| 1644 |
.cloned() |
| 1645 |
.collect(); |
| 1646 |
for language_server in language_servers { |
| 1647 |
let Some(request) = language_server.will_rename(old_path, &new_path, is_dir) else { |
| 1648 |
continue; |
| 1649 |
}; |
| 1650 |
let edit = match helix_lsp::block_on(request) { |
| 1651 |
Ok(edit) => edit.unwrap_or_default(), |
| 1652 |
Err(err) => { |
| 1653 |
log::error!("invalid willRename response: {err:?}"); |
| 1654 |
continue; |
| 1655 |
} |
| 1656 |
}; |
| 1657 |
if let Err(err) = self.apply_workspace_edit(language_server.offset_encoding(), &edit) { |
| 1658 |
log::error!("failed to apply workspace edit: {err:?}") |
| 1659 |
} |
| 1660 |
} |
| 1661 |
|
| 1662 |
if old_path.exists() { |
| 1663 |
fs::rename(old_path, &new_path)?; |
| 1664 |
} |
| 1665 |
|
| 1666 |
if let Some(doc) = self.document_by_path(old_path) { |
| 1667 |
self.set_doc_path(doc.id(), &new_path); |
| 1668 |
} |
| 1669 |
let is_dir = new_path.is_dir(); |
| 1670 |
for ls in self.language_servers.iter_clients() { |
| 1671 |
|
| 1672 |
|
| 1673 |
if !ls.is_initialized() { |
| 1674 |
continue; |
| 1675 |
} |
| 1676 |
ls.did_rename(old_path, &new_path, is_dir); |
| 1677 |
} |
| 1678 |
self.language_servers |
| 1679 |
.file_event_handler |
| 1680 |
.file_changed(old_path.to_owned()); |
| 1681 |
self.language_servers |
| 1682 |
.file_event_handler |
| 1683 |
.file_changed(new_path); |
| 1684 |
Ok(()) |
| 1685 |
} |
| 1686 |
|
| 1687 |
pub fn create_path(&mut self, path: &Path, is_dir: bool) -> io::Result<()> { |
| 1688 |
let path = canonicalize(path); |
| 1689 |
let language_servers: Vec<_> = self |
| 1690 |
.language_servers |
| 1691 |
.iter_clients() |
| 1692 |
.filter(|client| client.is_initialized()) |
| 1693 |
.cloned() |
| 1694 |
.collect(); |
| 1695 |
for language_server in language_servers { |
| 1696 |
let Some(request) = language_server.will_create(&path, is_dir) else { |
| 1697 |
continue; |
| 1698 |
}; |
| 1699 |
let edit = match helix_lsp::block_on(request) { |
| 1700 |
Ok(edit) => edit.unwrap_or_default(), |
| 1701 |
Err(err) => { |
| 1702 |
log::error!("invalid willCreate response: {err:?}"); |
| 1703 |
continue; |
| 1704 |
} |
| 1705 |
}; |
| 1706 |
if let Err(err) = self.apply_workspace_edit(language_server.offset_encoding(), &edit) { |
| 1707 |
log::error!("failed to apply workspace edit: {err:?}") |
| 1708 |
} |
| 1709 |
} |
| 1710 |
|
| 1711 |
if let Some(dir) = path.parent() { |
| 1712 |
if !dir.is_dir() { |
| 1713 |
fs::create_dir_all(dir)?; |
| 1714 |
} |
| 1715 |
} |
| 1716 |
if is_dir { |
| 1717 |
fs::create_dir(&path)?; |
| 1718 |
} else { |
| 1719 |
fs::write(&path, [])?; |
| 1720 |
} |
| 1721 |
|
| 1722 |
for ls in self.language_servers.iter_clients() { |
| 1723 |
if !ls.is_initialized() { |
| 1724 |
continue; |
| 1725 |
} |
| 1726 |
ls.did_create(&path, is_dir); |
| 1727 |
} |
| 1728 |
self.language_servers.file_event_handler.file_changed(path); |
| 1729 |
Ok(()) |
| 1730 |
} |
| 1731 |
|
| 1732 |
pub fn delete_path(&mut self, path: &Path, recursive: bool) -> io::Result<()> { |
| 1733 |
let path = canonicalize(path); |
| 1734 |
let is_dir = path.is_dir(); |
| 1735 |
let language_servers: Vec<_> = self |
| 1736 |
.language_servers |
| 1737 |
.iter_clients() |
| 1738 |
.filter(|client| client.is_initialized()) |
| 1739 |
.cloned() |
| 1740 |
.collect(); |
| 1741 |
for language_server in language_servers { |
| 1742 |
let Some(request) = language_server.will_delete(&path, is_dir) else { |
| 1743 |
continue; |
| 1744 |
}; |
| 1745 |
let edit = match helix_lsp::block_on(request) { |
| 1746 |
Ok(edit) => edit.unwrap_or_default(), |
| 1747 |
Err(err) => { |
| 1748 |
log::error!("invalid willDelete response: {err:?}"); |
| 1749 |
continue; |
| 1750 |
} |
| 1751 |
}; |
| 1752 |
if let Err(err) = self.apply_workspace_edit(language_server.offset_encoding(), &edit) { |
| 1753 |
log::error!("failed to apply workspace edit: {err:?}") |
| 1754 |
} |
| 1755 |
} |
| 1756 |
|
| 1757 |
if is_dir { |
| 1758 |
if recursive { |
| 1759 |
fs::remove_dir_all(&path)?; |
| 1760 |
} else { |
| 1761 |
fs::remove_dir(&path)?; |
| 1762 |
} |
| 1763 |
} else { |
| 1764 |
fs::remove_file(&path)?; |
| 1765 |
} |
| 1766 |
|
| 1767 |
for ls in self.language_servers.iter_clients() { |
| 1768 |
if !ls.is_initialized() { |
| 1769 |
continue; |
| 1770 |
} |
| 1771 |
ls.did_delete(&path, is_dir); |
| 1772 |
} |
| 1773 |
self.language_servers.file_event_handler.file_changed(path); |
| 1774 |
Ok(()) |
| 1775 |
} |
| 1776 |
|
| 1777 |
pub fn set_doc_path(&mut self, doc_id: DocumentId, path: &Path) { |
| 1778 |
let doc = doc_mut!(self, &doc_id); |
| 1779 |
let old_path = doc.path(); |
| 1780 |
|
| 1781 |
if let Some(old_path) = old_path { |
| 1782 |
|
| 1783 |
|
| 1784 |
if old_path == path { |
| 1785 |
return; |
| 1786 |
} |
| 1787 |
|
| 1788 |
for language_server in doc.language_servers() { |
| 1789 |
language_server.text_document_did_close(doc.identifier()); |
| 1790 |
} |
| 1791 |
} |
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
doc.language_servers.clear(); |
| 1797 |
doc.set_path(Some(path)); |
| 1798 |
doc.detect_editor_config(); |
| 1799 |
self.refresh_doc_language(doc_id) |
| 1800 |
} |
| 1801 |
|
| 1802 |
pub fn refresh_doc_language(&mut self, doc_id: DocumentId) { |
| 1803 |
let loader = self.syn_loader.load(); |
| 1804 |
let doc = doc_mut!(self, &doc_id); |
| 1805 |
doc.detect_language(&loader); |
| 1806 |
doc.detect_editor_config(); |
| 1807 |
doc.detect_indent_and_line_ending(); |
| 1808 |
self.refresh_language_servers(doc_id); |
| 1809 |
let doc = doc_mut!(self, &doc_id); |
| 1810 |
let diagnostics = Editor::doc_diagnostics(&self.language_servers, &self.diagnostics, doc); |
| 1811 |
doc.replace_diagnostics(diagnostics, &[], None); |
| 1812 |
doc.reset_all_inlay_hints(); |
| 1813 |
} |
| 1814 |
|
| 1815 |
|
| 1816 |
pub fn launch_language_servers(&mut self, doc_id: DocumentId) { |
| 1817 |
if !self.config().lsp.enable { |
| 1818 |
return; |
| 1819 |
} |
| 1820 |
|
| 1821 |
let Some(doc) = self.documents.get_mut(&doc_id) else { |
| 1822 |
return; |
| 1823 |
}; |
| 1824 |
let Some(doc_url) = doc.url() else { |
| 1825 |
return; |
| 1826 |
}; |
| 1827 |
let (lang, path) = (doc.language.clone(), doc.path()); |
| 1828 |
let config = doc.config.load(); |
| 1829 |
let root_dirs = &config.workspace_lsp_roots; |
| 1830 |
|
| 1831 |
let workspace = doc.workspace_root(); |
| 1832 |
let trust = self.workspace_trust.query(workspace, TrustQuery::Lsp); |
| 1833 |
if !trust.is_trusted() { |
| 1834 |
return; |
| 1835 |
} |
| 1836 |
|
| 1837 |
|
| 1838 |
let language_servers = lang.as_ref().map_or_else(HashMap::default, |language| { |
| 1839 |
self.language_servers |
| 1840 |
.get(language, path, root_dirs, config.lsp.snippets) |
| 1841 |
.filter_map(|(lang, client)| match client { |
| 1842 |
Ok(client) => Some((lang, client)), |
| 1843 |
Err(err) => { |
| 1844 |
if let helix_lsp::Error::ExecutableNotFound(err) = err { |
| 1845 |
|
| 1846 |
log::debug!( |
| 1847 |
"Language server not found for `{}` {} {}", language.scope, lang, err, |
| 1848 |
); |
| 1849 |
} else { |
| 1850 |
log::error!( |
| 1851 |
"Failed to initialize the language servers for `{}` - `{}` {{ {} }}", |
| 1852 |
language.scope, |
| 1853 |
lang, |
| 1854 |
err |
| 1855 |
); |
| 1856 |
} |
| 1857 |
None |
| 1858 |
} |
| 1859 |
}) |
| 1860 |
.collect::<HashMap<_, _>>() |
| 1861 |
}); |
| 1862 |
|
| 1863 |
if language_servers.is_empty() && doc.language_servers.is_empty() { |
| 1864 |
return; |
| 1865 |
} |
| 1866 |
|
| 1867 |
let language_id = doc.language_id().map(ToOwned::to_owned).unwrap_or_default(); |
| 1868 |
|
| 1869 |
|
| 1870 |
let doc_language_servers_not_in_registry = |
| 1871 |
doc.language_servers.iter().filter(|(name, doc_ls)| { |
| 1872 |
language_servers |
| 1873 |
.get(*name) |
| 1874 |
.is_none_or(|ls| ls.id() != doc_ls.id()) |
| 1875 |
}); |
| 1876 |
|
| 1877 |
for (_, language_server) in doc_language_servers_not_in_registry { |
| 1878 |
language_server.text_document_did_close(doc.identifier()); |
| 1879 |
} |
| 1880 |
|
| 1881 |
let language_servers_not_in_doc = language_servers.iter().filter(|(name, ls)| { |
| 1882 |
doc.language_servers |
| 1883 |
.get(*name) |
| 1884 |
.is_none_or(|doc_ls| ls.id() != doc_ls.id()) |
| 1885 |
}); |
| 1886 |
|
| 1887 |
for (_, language_server) in language_servers_not_in_doc { |
| 1888 |
|
| 1889 |
language_server.text_document_did_open( |
| 1890 |
doc_url.clone(), |
| 1891 |
doc.version(), |
| 1892 |
doc.text(), |
| 1893 |
language_id.clone(), |
| 1894 |
); |
| 1895 |
} |
| 1896 |
|
| 1897 |
doc.language_servers = language_servers; |
| 1898 |
} |
| 1899 |
|
| 1900 |
fn _refresh(&mut self) { |
| 1901 |
let config = self.config(); |
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
if !config.lsp.display_inlay_hints { |
| 1910 |
for doc in self.documents_mut() { |
| 1911 |
doc.reset_all_inlay_hints(); |
| 1912 |
} |
| 1913 |
} |
| 1914 |
|
| 1915 |
for (view, _) in self.tree.views_mut() { |
| 1916 |
let doc = doc_mut!(self, &view.doc); |
| 1917 |
view.sync_changes(doc); |
| 1918 |
view.gutters = config.gutters.clone(); |
| 1919 |
view.ensure_cursor_in_view(doc, config.scrolloff) |
| 1920 |
} |
| 1921 |
} |
| 1922 |
|
| 1923 |
fn replace_document_in_view(&mut self, current_view: ViewId, doc_id: DocumentId) { |
| 1924 |
let scrolloff = self.config().scrolloff; |
| 1925 |
let view = self.tree.get_mut(current_view); |
| 1926 |
|
| 1927 |
view.doc = doc_id; |
| 1928 |
let doc = doc_mut!(self, &doc_id); |
| 1929 |
|
| 1930 |
doc.ensure_view_init(view.id); |
| 1931 |
view.sync_changes(doc); |
| 1932 |
doc.mark_as_focused(); |
| 1933 |
|
| 1934 |
view.ensure_cursor_in_view(doc, scrolloff) |
| 1935 |
} |
| 1936 |
|
| 1937 |
pub fn switch(&mut self, id: DocumentId, action: Action) { |
| 1938 |
use crate::tree::Layout; |
| 1939 |
|
| 1940 |
if !self.documents.contains_key(&id) { |
| 1941 |
log::error!("cannot switch to document that does not exist (anymore)"); |
| 1942 |
return; |
| 1943 |
} |
| 1944 |
|
| 1945 |
if !matches!(action, Action::Load) { |
| 1946 |
self.enter_normal_mode(); |
| 1947 |
} |
| 1948 |
|
| 1949 |
let focust_lost = match action { |
| 1950 |
Action::Replace => { |
| 1951 |
let (view, doc) = current_ref!(self); |
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
let remove_empty_scratch = !doc.is_modified() |
| 1956 |
|
| 1957 |
&& doc.path().is_none() |
| 1958 |
|
| 1959 |
&& id != doc.id |
| 1960 |
|
| 1961 |
&& !self |
| 1962 |
.tree |
| 1963 |
.traverse() |
| 1964 |
.any(|(_, v)| v.doc == doc.id && v.id != view.id); |
| 1965 |
|
| 1966 |
let (view, doc) = current!(self); |
| 1967 |
let view_id = view.id; |
| 1968 |
|
| 1969 |
|
| 1970 |
doc.append_changes_to_history(view); |
| 1971 |
|
| 1972 |
if remove_empty_scratch { |
| 1973 |
|
| 1974 |
|
| 1975 |
let id = doc.id; |
| 1976 |
self.documents.remove(&id); |
| 1977 |
|
| 1978 |
|
| 1979 |
for (view, _) in self.tree.views_mut() { |
| 1980 |
view.remove_document(&id); |
| 1981 |
} |
| 1982 |
} else { |
| 1983 |
let jump = (view.doc, doc.selection(view.id).clone()); |
| 1984 |
view.push_jump(doc, jump); |
| 1985 |
|
| 1986 |
if doc.id != id { |
| 1987 |
view.add_to_history(view.doc); |
| 1988 |
|
| 1989 |
if std::mem::take(&mut doc.modified_since_accessed) |
| 1990 |
&& view.last_modified_docs[0] != Some(view.doc) |
| 1991 |
{ |
| 1992 |
view.last_modified_docs = [Some(view.doc), view.last_modified_docs[0]]; |
| 1993 |
} |
| 1994 |
} |
| 1995 |
} |
| 1996 |
|
| 1997 |
self.replace_document_in_view(view_id, id); |
| 1998 |
|
| 1999 |
dispatch(DocumentFocusLost { |
| 2000 |
editor: self, |
| 2001 |
doc: id, |
| 2002 |
}); |
| 2003 |
return; |
| 2004 |
} |
| 2005 |
Action::Load => { |
| 2006 |
let view_id = view!(self).id; |
| 2007 |
let doc = doc_mut!(self, &id); |
| 2008 |
doc.ensure_view_init(view_id); |
| 2009 |
doc.mark_as_focused(); |
| 2010 |
return; |
| 2011 |
} |
| 2012 |
Action::HorizontalSplit | Action::VerticalSplit => { |
| 2013 |
let focus_lost = self.tree.try_get(self.tree.focus).map(|view| view.doc); |
| 2014 |
|
| 2015 |
let view = self |
| 2016 |
.tree |
| 2017 |
.try_get(self.tree.focus) |
| 2018 |
.filter(|v| id == v.doc) |
| 2019 |
.cloned() |
| 2020 |
.unwrap_or_else(|| View::new(id, self.config().gutters.clone())); |
| 2021 |
let view_id = self.tree.split( |
| 2022 |
view, |
| 2023 |
match action { |
| 2024 |
Action::HorizontalSplit => Layout::Horizontal, |
| 2025 |
Action::VerticalSplit => Layout::Vertical, |
| 2026 |
_ => unreachable!(), |
| 2027 |
}, |
| 2028 |
); |
| 2029 |
|
| 2030 |
let doc = doc_mut!(self, &id); |
| 2031 |
doc.ensure_view_init(view_id); |
| 2032 |
doc.mark_as_focused(); |
| 2033 |
focus_lost |
| 2034 |
} |
| 2035 |
}; |
| 2036 |
|
| 2037 |
self._refresh(); |
| 2038 |
if let Some(focus_lost) = focust_lost { |
| 2039 |
dispatch(DocumentFocusLost { |
| 2040 |
editor: self, |
| 2041 |
doc: focus_lost, |
| 2042 |
}); |
| 2043 |
} |
| 2044 |
} |
| 2045 |
|
| 2046 |
|
| 2047 |
fn new_document(&mut self, mut doc: Document) -> DocumentId { |
| 2048 |
let id = self.next_document_id; |
| 2049 |
|
| 2050 |
self.next_document_id = |
| 2051 |
DocumentId(unsafe { NonZeroUsize::new_unchecked(self.next_document_id.0.get() + 1) }); |
| 2052 |
doc.id = id; |
| 2053 |
self.documents.insert(id, doc); |
| 2054 |
|
| 2055 |
let (save_sender, save_receiver) = tokio::sync::mpsc::unbounded_channel(); |
| 2056 |
self.saves.insert(id, save_sender); |
| 2057 |
|
| 2058 |
let stream = UnboundedReceiverStream::new(save_receiver).flatten(); |
| 2059 |
self.save_queue.push(stream); |
| 2060 |
|
| 2061 |
id |
| 2062 |
} |
| 2063 |
|
| 2064 |
fn new_file_from_document(&mut self, action: Action, doc: Document) -> DocumentId { |
| 2065 |
let id = self.new_document(doc); |
| 2066 |
self.switch(id, action); |
| 2067 |
id |
| 2068 |
} |
| 2069 |
|
| 2070 |
pub fn new_file(&mut self, action: Action) -> DocumentId { |
| 2071 |
self.new_file_from_document( |
| 2072 |
action, |
| 2073 |
Document::default(self.config.clone(), self.syn_loader.clone()), |
| 2074 |
) |
| 2075 |
} |
| 2076 |
|
| 2077 |
pub fn new_file_from_stdin(&mut self, action: Action) -> Result<DocumentId, Error> { |
| 2078 |
let (stdin, encoding, has_bom) = crate::document::read_to_string(&mut stdin(), None)?; |
| 2079 |
let doc = Document::from( |
| 2080 |
helix_core::Rope::default(), |
| 2081 |
Some((encoding, has_bom)), |
| 2082 |
self.config.clone(), |
| 2083 |
self.syn_loader.clone(), |
| 2084 |
); |
| 2085 |
let doc_id = self.new_file_from_document(action, doc); |
| 2086 |
let doc = doc_mut!(self, &doc_id); |
| 2087 |
let view = view_mut!(self); |
| 2088 |
doc.ensure_view_init(view.id); |
| 2089 |
let transaction = |
| 2090 |
helix_core::Transaction::insert(doc.text(), doc.selection(view.id), stdin.into()) |
| 2091 |
.with_selection(Selection::point(0)); |
| 2092 |
doc.apply(&transaction, view.id); |
| 2093 |
doc.append_changes_to_history(view); |
| 2094 |
Ok(doc_id) |
| 2095 |
} |
| 2096 |
|
| 2097 |
pub fn document_id_by_path(&self, path: &Path) -> Option<DocumentId> { |
| 2098 |
self.document_by_path(path).map(|doc| doc.id) |
| 2099 |
} |
| 2100 |
|
| 2101 |
|
| 2102 |
pub fn open(&mut self, path: &Path, action: Action) -> Result<DocumentId, DocumentOpenError> { |
| 2103 |
let path = helix_stdx::path::canonicalize(path); |
| 2104 |
let id = self.document_id_by_path(&path); |
| 2105 |
|
| 2106 |
let id = if let Some(id) = id { |
| 2107 |
id |
| 2108 |
} else { |
| 2109 |
let mut doc = Document::open( |
| 2110 |
&path, |
| 2111 |
None, |
| 2112 |
true, |
| 2113 |
self.config.clone(), |
| 2114 |
self.syn_loader.clone(), |
| 2115 |
)?; |
| 2116 |
|
| 2117 |
let diagnostics = |
| 2118 |
Editor::doc_diagnostics(&self.language_servers, &self.diagnostics, &doc); |
| 2119 |
doc.replace_diagnostics(diagnostics, &[], None); |
| 2120 |
|
| 2121 |
let trust_full = self |
| 2122 |
.workspace_trust |
| 2123 |
.query(doc.workspace_root(), TrustQuery::Git) |
| 2124 |
.is_trusted(); |
| 2125 |
if let Some(diff_base) = self.diff_providers.get_diff_base(&path, trust_full) { |
| 2126 |
doc.set_diff_base(diff_base); |
| 2127 |
} |
| 2128 |
doc.set_version_control_head( |
| 2129 |
self.diff_providers.get_current_head_name(&path, trust_full), |
| 2130 |
); |
| 2131 |
|
| 2132 |
let id = self.new_document(doc); |
| 2133 |
self.launch_language_servers(id); |
| 2134 |
|
| 2135 |
helix_event::dispatch(DocumentDidOpen { |
| 2136 |
editor: self, |
| 2137 |
doc: id, |
| 2138 |
}); |
| 2139 |
|
| 2140 |
id |
| 2141 |
}; |
| 2142 |
|
| 2143 |
self.switch(id, action); |
| 2144 |
|
| 2145 |
Ok(id) |
| 2146 |
} |
| 2147 |
|
| 2148 |
pub fn close(&mut self, id: ViewId) { |
| 2149 |
|
| 2150 |
for doc in self.documents_mut() { |
| 2151 |
doc.remove_view(id); |
| 2152 |
} |
| 2153 |
self.tree.remove(id); |
| 2154 |
self._refresh(); |
| 2155 |
} |
| 2156 |
|
| 2157 |
pub fn close_document(&mut self, doc_id: DocumentId, force: bool) -> Result<(), CloseError> { |
| 2158 |
let doc = match self.documents.get(&doc_id) { |
| 2159 |
Some(doc) => doc, |
| 2160 |
None => return Err(CloseError::DoesNotExist), |
| 2161 |
}; |
| 2162 |
if !force && doc.is_modified() { |
| 2163 |
return Err(CloseError::BufferModified(doc.display_name().into_owned())); |
| 2164 |
} |
| 2165 |
|
| 2166 |
|
| 2167 |
self.saves.remove(&doc_id); |
| 2168 |
|
| 2169 |
enum Action { |
| 2170 |
Close(ViewId), |
| 2171 |
ReplaceDoc(ViewId, DocumentId), |
| 2172 |
} |
| 2173 |
|
| 2174 |
let actions: Vec<Action> = self |
| 2175 |
.tree |
| 2176 |
.views_mut() |
| 2177 |
.filter_map(|(view, _focus)| { |
| 2178 |
view.remove_document(&doc_id); |
| 2179 |
|
| 2180 |
if view.doc == doc_id { |
| 2181 |
|
| 2182 |
if let Some(prev_doc) = view.docs_access_history.pop() { |
| 2183 |
Some(Action::ReplaceDoc(view.id, prev_doc)) |
| 2184 |
} else { |
| 2185 |
|
| 2186 |
Some(Action::Close(view.id)) |
| 2187 |
} |
| 2188 |
} else { |
| 2189 |
None |
| 2190 |
} |
| 2191 |
}) |
| 2192 |
.collect(); |
| 2193 |
|
| 2194 |
for action in actions { |
| 2195 |
match action { |
| 2196 |
Action::Close(view_id) => { |
| 2197 |
self.close(view_id); |
| 2198 |
} |
| 2199 |
Action::ReplaceDoc(view_id, doc_id) => { |
| 2200 |
self.replace_document_in_view(view_id, doc_id); |
| 2201 |
} |
| 2202 |
} |
| 2203 |
} |
| 2204 |
|
| 2205 |
let doc = self.documents.remove(&doc_id).unwrap(); |
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
|
| 2210 |
if self.tree.views().next().is_none() { |
| 2211 |
let doc_id = self |
| 2212 |
.documents |
| 2213 |
.iter() |
| 2214 |
.map(|(&doc_id, _)| doc_id) |
| 2215 |
.next() |
| 2216 |
.unwrap_or_else(|| { |
| 2217 |
self.new_document(Document::default( |
| 2218 |
self.config.clone(), |
| 2219 |
self.syn_loader.clone(), |
| 2220 |
)) |
| 2221 |
}); |
| 2222 |
let view = View::new(doc_id, self.config().gutters.clone()); |
| 2223 |
let view_id = self.tree.insert(view); |
| 2224 |
let doc = doc_mut!(self, &doc_id); |
| 2225 |
doc.ensure_view_init(view_id); |
| 2226 |
doc.mark_as_focused(); |
| 2227 |
} |
| 2228 |
|
| 2229 |
self._refresh(); |
| 2230 |
|
| 2231 |
helix_event::dispatch(DocumentDidClose { editor: self, doc }); |
| 2232 |
|
| 2233 |
Ok(()) |
| 2234 |
} |
| 2235 |
|
| 2236 |
pub fn save<P: Into<PathBuf>>( |
| 2237 |
&mut self, |
| 2238 |
doc_id: DocumentId, |
| 2239 |
path: Option<P>, |
| 2240 |
force: bool, |
| 2241 |
) -> anyhow::Result<()> { |
| 2242 |
|
| 2243 |
|
| 2244 |
|
| 2245 |
let path = path.map(|path| path.into()); |
| 2246 |
let doc = doc_mut!(self, &doc_id); |
| 2247 |
let doc_save_future = doc.save(path, force)?; |
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
let handler = self.language_servers.file_event_handler.clone(); |
| 2252 |
let future = async move { |
| 2253 |
let res = doc_save_future.await; |
| 2254 |
if let Ok(event) = &res { |
| 2255 |
handler.file_changed(event.path.clone()); |
| 2256 |
} |
| 2257 |
res |
| 2258 |
}; |
| 2259 |
|
| 2260 |
use futures_util::stream; |
| 2261 |
|
| 2262 |
self.saves |
| 2263 |
.get(&doc_id) |
| 2264 |
.ok_or_else(|| anyhow::format_err!("saves are closed for this document!"))? |
| 2265 |
.send(stream::once(Box::pin(future))) |
| 2266 |
.map_err(|err| anyhow!("failed to send save event: {}", err))?; |
| 2267 |
|
| 2268 |
self.write_count += 1; |
| 2269 |
|
| 2270 |
Ok(()) |
| 2271 |
} |
| 2272 |
|
| 2273 |
pub fn resize(&mut self, area: Rect) { |
| 2274 |
if self.tree.resize(area) { |
| 2275 |
self._refresh(); |
| 2276 |
}; |
| 2277 |
} |
| 2278 |
|
| 2279 |
pub fn focus(&mut self, view_id: ViewId) { |
| 2280 |
if self.tree.focus == view_id { |
| 2281 |
return; |
| 2282 |
} |
| 2283 |
|
| 2284 |
|
| 2285 |
self.enter_normal_mode(); |
| 2286 |
let (view, doc) = current!(self); |
| 2287 |
doc.append_changes_to_history(view); |
| 2288 |
self.ensure_cursor_in_view(view_id); |
| 2289 |
|
| 2290 |
for (view, _focused) in self.tree.views_mut() { |
| 2291 |
let doc = doc_mut!(self, &view.doc); |
| 2292 |
view.sync_changes(doc); |
| 2293 |
} |
| 2294 |
|
| 2295 |
let prev_id = std::mem::replace(&mut self.tree.focus, view_id); |
| 2296 |
doc_mut!(self).mark_as_focused(); |
| 2297 |
|
| 2298 |
let focus_lost = self.tree.get(prev_id).doc; |
| 2299 |
dispatch(DocumentFocusLost { |
| 2300 |
editor: self, |
| 2301 |
doc: focus_lost, |
| 2302 |
}); |
| 2303 |
} |
| 2304 |
|
| 2305 |
pub fn focus_next(&mut self) { |
| 2306 |
self.focus(self.tree.next()); |
| 2307 |
} |
| 2308 |
|
| 2309 |
pub fn focus_prev(&mut self) { |
| 2310 |
self.focus(self.tree.prev()); |
| 2311 |
} |
| 2312 |
|
| 2313 |
pub fn focus_direction(&mut self, direction: tree::Direction) { |
| 2314 |
let current_view = self.tree.focus; |
| 2315 |
if let Some(id) = self.tree.find_split_in_direction(current_view, direction) { |
| 2316 |
self.focus(id) |
| 2317 |
} |
| 2318 |
} |
| 2319 |
|
| 2320 |
pub fn swap_split_in_direction(&mut self, direction: tree::Direction) { |
| 2321 |
self.tree.swap_split_in_direction(direction); |
| 2322 |
} |
| 2323 |
|
| 2324 |
pub fn transpose_view(&mut self) { |
| 2325 |
self.tree.transpose(); |
| 2326 |
} |
| 2327 |
|
| 2328 |
pub fn should_close(&self) -> bool { |
| 2329 |
self.tree.is_empty() |
| 2330 |
} |
| 2331 |
|
| 2332 |
pub fn ensure_cursor_in_view(&mut self, id: ViewId) { |
| 2333 |
let config = self.config(); |
| 2334 |
let view = self.tree.get(id); |
| 2335 |
let doc = doc_mut!(self, &view.doc); |
| 2336 |
view.ensure_cursor_in_view(doc, config.scrolloff) |
| 2337 |
} |
| 2338 |
|
| 2339 |
#[inline] |
| 2340 |
pub fn document(&self, id: DocumentId) -> Option<&Document> { |
| 2341 |
self.documents.get(&id) |
| 2342 |
} |
| 2343 |
|
| 2344 |
#[inline] |
| 2345 |
pub fn document_mut(&mut self, id: DocumentId) -> Option<&mut Document> { |
| 2346 |
self.documents.get_mut(&id) |
| 2347 |
} |
| 2348 |
|
| 2349 |
#[inline] |
| 2350 |
pub fn documents(&self) -> impl Iterator<Item = &Document> { |
| 2351 |
self.documents.values() |
| 2352 |
} |
| 2353 |
|
| 2354 |
#[inline] |
| 2355 |
pub fn documents_mut(&mut self) -> impl Iterator<Item = &mut Document> { |
| 2356 |
self.documents.values_mut() |
| 2357 |
} |
| 2358 |
|
| 2359 |
pub fn document_by_path<P: AsRef<Path>>(&self, path: P) -> Option<&Document> { |
| 2360 |
self.documents() |
| 2361 |
.find(|doc| doc.path().is_some_and(|p| p == path.as_ref())) |
| 2362 |
} |
| 2363 |
|
| 2364 |
pub fn document_by_path_mut<P: AsRef<Path>>(&mut self, path: P) -> Option<&mut Document> { |
| 2365 |
self.documents_mut() |
| 2366 |
.find(|doc| doc.path().is_some_and(|p| p == path.as_ref())) |
| 2367 |
} |
| 2368 |
|
| 2369 |
|
| 2370 |
pub fn doc_diagnostics<'a>( |
| 2371 |
language_servers: &'a helix_lsp::Registry, |
| 2372 |
diagnostics: &'a Diagnostics, |
| 2373 |
document: &Document, |
| 2374 |
) -> impl Iterator<Item = helix_core::Diagnostic> + 'a { |
| 2375 |
Editor::doc_diagnostics_with_filter(language_servers, diagnostics, document, |_, _| true) |
| 2376 |
} |
| 2377 |
|
| 2378 |
|
| 2379 |
|
| 2380 |
pub fn doc_diagnostics_with_filter<'a>( |
| 2381 |
language_servers: &'a helix_lsp::Registry, |
| 2382 |
diagnostics: &'a Diagnostics, |
| 2383 |
document: &Document, |
| 2384 |
filter: impl Fn(&lsp::Diagnostic, &DiagnosticProvider) -> bool + 'a, |
| 2385 |
) -> impl Iterator<Item = helix_core::Diagnostic> + 'a { |
| 2386 |
let text = document.text().clone(); |
| 2387 |
let language_config = document.language.clone(); |
| 2388 |
document |
| 2389 |
.uri() |
| 2390 |
.and_then(|uri| diagnostics.get(&uri)) |
| 2391 |
.map(|diags| { |
| 2392 |
diags.iter().filter_map(move |(diagnostic, provider)| { |
| 2393 |
let server_id = provider.language_server_id()?; |
| 2394 |
let ls = language_servers.get_by_id(server_id)?; |
| 2395 |
language_config |
| 2396 |
.as_ref() |
| 2397 |
.and_then(|c| { |
| 2398 |
c.language_servers.iter().find(|features| { |
| 2399 |
features.name == ls.name() |
| 2400 |
&& features.has_feature(LanguageServerFeature::Diagnostics) |
| 2401 |
}) |
| 2402 |
}) |
| 2403 |
.and_then(|_| { |
| 2404 |
if filter(diagnostic, provider) { |
| 2405 |
Document::lsp_diagnostic_to_diagnostic( |
| 2406 |
&text, |
| 2407 |
language_config.as_deref(), |
| 2408 |
diagnostic, |
| 2409 |
provider.clone(), |
| 2410 |
ls.offset_encoding(), |
| 2411 |
) |
| 2412 |
} else { |
| 2413 |
None |
| 2414 |
} |
| 2415 |
}) |
| 2416 |
}) |
| 2417 |
}) |
| 2418 |
.into_iter() |
| 2419 |
.flatten() |
| 2420 |
} |
| 2421 |
|
| 2422 |
|
| 2423 |
|
| 2424 |
pub fn cursor(&self) -> (Option<Position>, CursorKind) { |
| 2425 |
let config = self.config(); |
| 2426 |
let (view, doc) = current_ref!(self); |
| 2427 |
if let Some(mut pos) = self.cursor_cache.get(view, doc) { |
| 2428 |
let inner = view.inner_area(doc); |
| 2429 |
pos.col += inner.x as usize; |
| 2430 |
pos.row += inner.y as usize; |
| 2431 |
let cursorkind = config.cursor_shape.from_mode(self.mode); |
| 2432 |
(Some(pos), cursorkind) |
| 2433 |
} else { |
| 2434 |
(None, CursorKind::default()) |
| 2435 |
} |
| 2436 |
} |
| 2437 |
|
| 2438 |
|
| 2439 |
|
| 2440 |
pub async fn close_language_servers(&self, timeout: Option<u64>) { |
| 2441 |
|
| 2442 |
|
| 2443 |
for client in self.language_servers.iter_clients() { |
| 2444 |
self.language_servers |
| 2445 |
.file_event_handler |
| 2446 |
.remove_client(client.id()); |
| 2447 |
} |
| 2448 |
|
| 2449 |
|
| 2450 |
for client in self.language_servers.iter_clients() { |
| 2451 |
client.force_shutdown(); |
| 2452 |
} |
| 2453 |
|
| 2454 |
|
| 2455 |
|
| 2456 |
|
| 2457 |
|
| 2458 |
|
| 2459 |
|
| 2460 |
let cap = Duration::from_millis(timeout.unwrap_or(1000)); |
| 2461 |
let _ = tokio::time::timeout(cap, async { |
| 2462 |
for client in self.language_servers.iter_clients() { |
| 2463 |
client.wait_shutdown_flushed().await; |
| 2464 |
} |
| 2465 |
}) |
| 2466 |
.await; |
| 2467 |
} |
| 2468 |
|
| 2469 |
pub async fn wait_event(&mut self) -> EditorEvent { |
| 2470 |
|
| 2471 |
|
| 2472 |
loop { |
| 2473 |
tokio::select! { |
| 2474 |
biased; |
| 2475 |
|
| 2476 |
Some(event) = self.save_queue.next() => { |
| 2477 |
self.write_count -= 1; |
| 2478 |
return EditorEvent::DocumentSaved(event) |
| 2479 |
} |
| 2480 |
Some(config_event) = self.config_events.1.recv() => { |
| 2481 |
return EditorEvent::ConfigEvent(config_event) |
| 2482 |
} |
| 2483 |
Some(message) = self.language_servers.incoming.next() => { |
| 2484 |
return EditorEvent::LanguageServerMessage(message) |
| 2485 |
} |
| 2486 |
Some(event) = self.debug_adapters.incoming.next() => { |
| 2487 |
return EditorEvent::DebuggerEvent(event) |
| 2488 |
} |
| 2489 |
|
| 2490 |
_ = helix_event::redraw_requested() => { |
| 2491 |
if !self.needs_redraw{ |
| 2492 |
self.needs_redraw = true; |
| 2493 |
let timeout = Instant::now() + Duration::from_millis(33); |
| 2494 |
if timeout < self.idle_timer.deadline() && timeout < self.redraw_timer.deadline(){ |
| 2495 |
self.redraw_timer.as_mut().reset(timeout) |
| 2496 |
} |
| 2497 |
} |
| 2498 |
} |
| 2499 |
|
| 2500 |
_ = &mut self.redraw_timer => { |
| 2501 |
self.redraw_timer.as_mut().reset(Instant::now() + Duration::from_secs(86400 * 365 * 30)); |
| 2502 |
return EditorEvent::Redraw |
| 2503 |
} |
| 2504 |
_ = &mut self.idle_timer => { |
| 2505 |
return EditorEvent::IdleTimer |
| 2506 |
} |
| 2507 |
} |
| 2508 |
} |
| 2509 |
} |
| 2510 |
|
| 2511 |
pub async fn flush_writes(&mut self) -> anyhow::Result<()> { |
| 2512 |
while self.write_count > 0 { |
| 2513 |
if let Some(save_event) = self.save_queue.next().await { |
| 2514 |
self.write_count -= 1; |
| 2515 |
|
| 2516 |
let save_event = match save_event { |
| 2517 |
Ok(event) => event, |
| 2518 |
Err(err) => { |
| 2519 |
self.set_error(err.to_string()); |
| 2520 |
bail!(err); |
| 2521 |
} |
| 2522 |
}; |
| 2523 |
|
| 2524 |
let doc = doc_mut!(self, &save_event.doc_id); |
| 2525 |
doc.set_last_saved_revision(save_event.revision, save_event.save_time); |
| 2526 |
} |
| 2527 |
} |
| 2528 |
|
| 2529 |
Ok(()) |
| 2530 |
} |
| 2531 |
|
| 2532 |
|
| 2533 |
pub fn enter_normal_mode(&mut self) { |
| 2534 |
use helix_core::graphemes; |
| 2535 |
|
| 2536 |
if self.mode == Mode::Normal { |
| 2537 |
return; |
| 2538 |
} |
| 2539 |
|
| 2540 |
self.mode = Mode::Normal; |
| 2541 |
let (view, doc) = current!(self); |
| 2542 |
|
| 2543 |
try_restore_indent(doc, view); |
| 2544 |
|
| 2545 |
|
| 2546 |
if doc.restore_cursor { |
| 2547 |
let text = doc.text().slice(..); |
| 2548 |
let selection = doc.selection(view.id).clone().transform(|range| { |
| 2549 |
let mut head = range.to(); |
| 2550 |
if range.head > range.anchor { |
| 2551 |
head = graphemes::prev_grapheme_boundary(text, head); |
| 2552 |
} |
| 2553 |
|
| 2554 |
Range::new(range.from(), head) |
| 2555 |
}); |
| 2556 |
|
| 2557 |
doc.set_selection(view.id, selection); |
| 2558 |
doc.restore_cursor = false; |
| 2559 |
} |
| 2560 |
} |
| 2561 |
|
| 2562 |
pub fn current_stack_frame(&self) -> Option<&dap::StackFrame> { |
| 2563 |
self.debug_adapters.current_stack_frame() |
| 2564 |
} |
| 2565 |
|
| 2566 |
|
| 2567 |
|
| 2568 |
|
| 2569 |
|
| 2570 |
pub fn get_synced_view_id(&mut self, id: DocumentId) -> ViewId { |
| 2571 |
let current_view = view_mut!(self); |
| 2572 |
let doc = self.documents.get_mut(&id).unwrap(); |
| 2573 |
if doc.selections().contains_key(¤t_view.id) { |
| 2574 |
|
| 2575 |
if current_view.doc != id { |
| 2576 |
current_view.sync_changes(doc); |
| 2577 |
} |
| 2578 |
current_view.id |
| 2579 |
} else if let Some(view_id) = doc.selections().keys().next() { |
| 2580 |
let view_id = *view_id; |
| 2581 |
let view = self.tree.get_mut(view_id); |
| 2582 |
view.sync_changes(doc); |
| 2583 |
view_id |
| 2584 |
} else { |
| 2585 |
doc.ensure_view_init(current_view.id); |
| 2586 |
current_view.id |
| 2587 |
} |
| 2588 |
} |
| 2589 |
|
| 2590 |
pub fn set_cwd(&mut self, path: &Path) -> std::io::Result<()> { |
| 2591 |
self.last_cwd = helix_stdx::env::set_current_working_dir(path)?; |
| 2592 |
self.clear_doc_relative_paths(); |
| 2593 |
Ok(()) |
| 2594 |
} |
| 2595 |
|
| 2596 |
pub fn get_last_cwd(&mut self) -> Option<&Path> { |
| 2597 |
self.last_cwd.as_deref() |
| 2598 |
} |
| 2599 |
|
| 2600 |
pub fn jump_forward(&mut self, view_id: ViewId, count: usize) { |
| 2601 |
if let Some((doc_id, selection)) = view_mut!(self, view_id).jumps.forward(count).cloned() { |
| 2602 |
self.jump_to(view_id, doc_id, selection); |
| 2603 |
} |
| 2604 |
} |
| 2605 |
|
| 2606 |
pub fn jump_backward(&mut self, view_id: ViewId, count: usize) { |
| 2607 |
let view = view_mut!(self, view_id); |
| 2608 |
let doc = doc_mut!(self, &view.doc); |
| 2609 |
|
| 2610 |
|
| 2611 |
|
| 2612 |
|
| 2613 |
view.sync_changes(doc); |
| 2614 |
if let Some((doc_id, selection)) = view.jumps.backward(view_id, doc, count).cloned() { |
| 2615 |
self.jump_to(view_id, doc_id, selection); |
| 2616 |
} |
| 2617 |
} |
| 2618 |
|
| 2619 |
fn jump_to(&mut self, view_id: ViewId, dest_doc_id: DocumentId, mut selection: Selection) { |
| 2620 |
let view = view_mut!(self, view_id); |
| 2621 |
let old_doc_id = view.doc; |
| 2622 |
if old_doc_id != dest_doc_id { |
| 2623 |
let new_doc = doc_mut!(self, &dest_doc_id); |
| 2624 |
if let Some(transaction) = view.changes_to_sync(new_doc) { |
| 2625 |
let text = new_doc.text().slice(..); |
| 2626 |
selection = selection.map(transaction.changes()).ensure_invariants(text); |
| 2627 |
} |
| 2628 |
self.replace_document_in_view(view_id, dest_doc_id); |
| 2629 |
dispatch(DocumentFocusLost { |
| 2630 |
editor: self, |
| 2631 |
doc: old_doc_id, |
| 2632 |
}); |
| 2633 |
} |
| 2634 |
let (view, doc) = current!(self); |
| 2635 |
doc.set_selection(view_id, selection); |
| 2636 |
view.ensure_cursor_in_view_center(doc, self.config.load().scrolloff); |
| 2637 |
} |
| 2638 |
} |
| 2639 |
|
| 2640 |
fn try_restore_indent(doc: &mut Document, view: &mut View) { |
| 2641 |
use helix_core::{ |
| 2642 |
chars::char_is_whitespace, |
| 2643 |
line_ending::{line_end_char_index, str_is_line_ending}, |
| 2644 |
unicode::segmentation::UnicodeSegmentation, |
| 2645 |
Operation, Transaction, |
| 2646 |
}; |
| 2647 |
|
| 2648 |
fn inserted_a_new_blank_line(changes: &[Operation], pos: usize, line_end_pos: usize) -> bool { |
| 2649 |
if let [Operation::Retain(move_pos), Operation::Insert(ref inserted_str), Operation::Retain(_)] = |
| 2650 |
changes |
| 2651 |
{ |
| 2652 |
let mut graphemes = inserted_str.graphemes(true); |
| 2653 |
move_pos + inserted_str.len() == pos |
| 2654 |
&& graphemes.next().is_some_and(str_is_line_ending) |
| 2655 |
&& graphemes.all(|g| g.chars().all(char_is_whitespace)) |
| 2656 |
&& pos == line_end_pos |
| 2657 |
} else { |
| 2658 |
false |
| 2659 |
} |
| 2660 |
} |
| 2661 |
|
| 2662 |
let doc_changes = doc.changes().changes(); |
| 2663 |
let text = doc.text().slice(..); |
| 2664 |
let range = doc.selection(view.id).primary(); |
| 2665 |
let pos = range.cursor(text); |
| 2666 |
let line_end_pos = line_end_char_index(&text, range.cursor_line(text)); |
| 2667 |
|
| 2668 |
if inserted_a_new_blank_line(doc_changes, pos, line_end_pos) { |
| 2669 |
|
| 2670 |
let line_start_pos = text.line_to_char(range.cursor_line(text)); |
| 2671 |
let transaction = |
| 2672 |
Transaction::change(doc.text(), [(line_start_pos, pos, None)].into_iter()); |
| 2673 |
doc.apply(&transaction, view.id); |
| 2674 |
} |
| 2675 |
} |
| 2676 |
|
| 2677 |
#[derive(Default)] |
| 2678 |
pub struct CursorCache(Cell<Option<Option<Position>>>); |
| 2679 |
|
| 2680 |
impl CursorCache { |
| 2681 |
pub fn get(&self, view: &View, doc: &Document) -> Option<Position> { |
| 2682 |
if let Some(pos) = self.0.get() { |
| 2683 |
return pos; |
| 2684 |
} |
| 2685 |
|
| 2686 |
let text = doc.text().slice(..); |
| 2687 |
let cursor = doc.selection(view.id).primary().cursor(text); |
| 2688 |
let res = view.screen_coords_at_pos(doc, text, cursor); |
| 2689 |
self.set(res); |
| 2690 |
res |
| 2691 |
} |
| 2692 |
|
| 2693 |
pub fn set(&self, cursor_pos: Option<Position>) { |
| 2694 |
self.0.set(Some(cursor_pos)) |
| 2695 |
} |
| 2696 |
|
| 2697 |
pub fn reset(&self) { |
| 2698 |
self.0.set(None) |
| 2699 |
} |
| 2700 |
} |