on master help sign in
Document more of document.rs (#562)
16bf8e1Kirawi
helix-view/src/document.rs+27
@@ -20,6 +20,7 @@ use helix_lsp::util::LspFormatting;
20
20
21use crate::{DocumentId, Theme, ViewId};
21use crate::{DocumentId, Theme, ViewId};
22
22
23+/// 8kB of buffer space for encoding and decoding `Rope`s.
23const BUF_SIZE: usize = 8192;
24const BUF_SIZE: usize = 8192;
24
25
25#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
26#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
@@ -437,6 +438,7 @@ impl Document {
437 theme: Option<&Theme>,
438 theme: Option<&Theme>,
438 config_loader: Option<&syntax::Loader>,
439 config_loader: Option<&syntax::Loader>,
439 ) -> Result<Self, Error> {
440 ) -> Result<Self, Error> {
441+ // Open the file if it exists, otherwise assume it is a new file (and thus empty).
440 let (rope, encoding) = if path.exists() {
442 let (rope, encoding) = if path.exists() {
441 let mut file =
443 let mut file =
442 std::fs::File::open(path).context(format!("unable to open {:?}", path))?;
444 std::fs::File::open(path).context(format!("unable to open {:?}", path))?;
@@ -564,6 +566,7 @@ impl Document {
564 }
566 }
565 }
567 }
566
568
569+ /// Detect the programming language based on the file type.
567 pub fn detect_language(&mut self, theme: Option<&Theme>, config_loader: &syntax::Loader) {
570 pub fn detect_language(&mut self, theme: Option<&Theme>, config_loader: &syntax::Loader) {
568 if let Some(path) = &self.path {
571 if let Some(path) = &self.path {
569 let language_config = config_loader.language_config_for_file_name(path);
572 let language_config = config_loader.language_config_for_file_name(path);
@@ -571,6 +574,10 @@ impl Document {
571 }
574 }
572 }
575 }
573
576
577+ /// Detect the indentation used in the file, or otherwise defaults to the language indentation
578+ /// configured in `languages.toml`, with a fallback back to 2 space indentation if it isn't
579+ /// specified. Line ending is likewise auto-detected, and will fallback to the default OS
580+ /// line ending.
574 pub fn detect_indent_and_line_ending(&mut self) {
581 pub fn detect_indent_and_line_ending(&mut self) {
575 self.indent_style = auto_detect_indent_style(&self.text).unwrap_or_else(|| {
582 self.indent_style = auto_detect_indent_style(&self.text).unwrap_or_else(|| {
576 IndentStyle::from_str(
583 IndentStyle::from_str(
@@ -596,6 +603,9 @@ impl Document {
596 let mut file = std::fs::File::open(path.unwrap())?;
603 let mut file = std::fs::File::open(path.unwrap())?;
597 let (rope, ..) = from_reader(&mut file, Some(encoding))?;
604 let (rope, ..) = from_reader(&mut file, Some(encoding))?;
598
605
606+ // Calculate the difference between the buffer and source text, and apply it.
607+ // This is not considered a modification of the contents of the file regardless
608+ // of the encoding.
599 let transaction = helix_core::diff::compare_ropes(self.text(), &rope);
609 let transaction = helix_core::diff::compare_ropes(self.text(), &rope);
600 self.apply(&transaction, view_id);
610 self.apply(&transaction, view_id);
601 self.append_changes_to_history(view_id);
611 self.append_changes_to_history(view_id);
@@ -630,6 +640,8 @@ impl Document {
630 Ok(())
640 Ok(())
631 }
641 }
632
642
643+ /// Set the programming language for the file and load associated data (e.g. highlighting)
644+ /// if it exists.
633 pub fn set_language(
645 pub fn set_language(
634 &mut self,
646 &mut self,
635 theme: Option<&Theme>,
647 theme: Option<&Theme>,
@@ -650,6 +662,8 @@ impl Document {
650 };
662 };
651 }
663 }
652
664
665+ /// Set the programming language for the file if you know the name (scope) but don't have the
666+ /// [`syntax::LanguageConfiguration`] for it.
653 pub fn set_language2(
667 pub fn set_language2(
654 &mut self,
668 &mut self,
655 scope: &str,
669 scope: &str,
@@ -661,16 +675,19 @@ impl Document {
661 self.set_language(theme, language_config);
675 self.set_language(theme, language_config);
662 }
676 }
663
677
678+ /// Set the LSP.
664 pub fn set_language_server(&mut self, language_server: Option<Arc<helix_lsp::Client>>) {
679 pub fn set_language_server(&mut self, language_server: Option<Arc<helix_lsp::Client>>) {
665 self.language_server = language_server;
680 self.language_server = language_server;
666 }
681 }
667
682
683+ /// Select text within the [`Document`].
668 pub fn set_selection(&mut self, view_id: ViewId, selection: Selection) {
684 pub fn set_selection(&mut self, view_id: ViewId, selection: Selection) {
669 // TODO: use a transaction?
685 // TODO: use a transaction?
670 self.selections
686 self.selections
671 .insert(view_id, selection.ensure_invariants(self.text().slice(..)));
687 .insert(view_id, selection.ensure_invariants(self.text().slice(..)));
672 }
688 }
673
689
690+ /// Apply a [`Transaction`] to the [`Document`] to change its text.
674 fn apply_impl(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
691 fn apply_impl(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
675 let old_doc = self.text().clone();
692 let old_doc = self.text().clone();
676
693
@@ -733,6 +750,7 @@ impl Document {
733 success
750 success
734 }
751 }
735
752
753+ /// Apply a [`Transaction`] to the [`Document`] to change its text.
736 pub fn apply(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
754 pub fn apply(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
737 // store the state just before any changes are made. This allows us to undo to the
755 // store the state just before any changes are made. This allows us to undo to the
738 // state just before a transaction was applied.
756 // state just before a transaction was applied.
@@ -754,6 +772,7 @@ impl Document {
754 success
772 success
755 }
773 }
756
774
775+ /// Undo the last modification to the [`Document`].
757 pub fn undo(&mut self, view_id: ViewId) {
776 pub fn undo(&mut self, view_id: ViewId) {
758 let mut history = self.history.take();
777 let mut history = self.history.take();
759 let success = if let Some(transaction) = history.undo() {
778 let success = if let Some(transaction) = history.undo() {
@@ -769,6 +788,7 @@ impl Document {
769 }
788 }
770 }
789 }
771
790
791+ /// Redo the last modification to the [`Document`].
772 pub fn redo(&mut self, view_id: ViewId) {
792 pub fn redo(&mut self, view_id: ViewId) {
773 let mut history = self.history.take();
793 let mut history = self.history.take();
774 let success = if let Some(transaction) = history.redo() {
794 let success = if let Some(transaction) = history.redo() {
@@ -784,6 +804,7 @@ impl Document {
784 }
804 }
785 }
805 }
786
806
807+ /// Undo modifications to the [`Document`] according to `uk`.
787 pub fn earlier(&mut self, view_id: ViewId, uk: helix_core::history::UndoKind) {
808 pub fn earlier(&mut self, view_id: ViewId, uk: helix_core::history::UndoKind) {
788 let txns = self.history.get_mut().earlier(uk);
809 let txns = self.history.get_mut().earlier(uk);
789 for txn in txns {
810 for txn in txns {
@@ -791,6 +812,7 @@ impl Document {
791 }
812 }
792 }
813 }
793
814
815+ /// Redo modifications to the [`Document`] according to `uk`.
794 pub fn later(&mut self, view_id: ViewId, uk: helix_core::history::UndoKind) {
816 pub fn later(&mut self, view_id: ViewId, uk: helix_core::history::UndoKind) {
795 let txns = self.history.get_mut().later(uk);
817 let txns = self.history.get_mut().later(uk);
796 for txn in txns {
818 for txn in txns {
@@ -823,6 +845,7 @@ impl Document {
823 self.id
845 self.id
824 }
846 }
825
847
848+ /// If there are unsaved modifications.
826 pub fn is_modified(&self) -> bool {
849 pub fn is_modified(&self) -> bool {
827 let history = self.history.take();
850 let history = self.history.take();
828 let current_revision = history.current_revision();
851 let current_revision = history.current_revision();
@@ -830,6 +853,7 @@ impl Document {
830 current_revision != self.last_saved_revision || !self.changes.is_empty()
853 current_revision != self.last_saved_revision || !self.changes.is_empty()
831 }
854 }
832
855
856+ /// Save modifications to history, and so [`Self::is_modified`] will return false.
833 pub fn reset_modified(&mut self) {
857 pub fn reset_modified(&mut self) {
834 let history = self.history.take();
858 let history = self.history.take();
835 let current_revision = history.current_revision();
859 let current_revision = history.current_revision();
@@ -837,6 +861,7 @@ impl Document {
837 self.last_saved_revision = current_revision;
861 self.last_saved_revision = current_revision;
838 }
862 }
839
863
864+ /// Current editing mode for the [`Document`].
840 pub fn mode(&self) -> Mode {
865 pub fn mode(&self) -> Mode {
841 self.mode
866 self.mode
842 }
867 }
@@ -848,6 +873,7 @@ impl Document {
848 .map(|language| language.scope.as_str())
873 .map(|language| language.scope.as_str())
849 }
874 }
850
875
876+ /// Corresponding [`LanguageConfiguration`].
851 pub fn language_config(&self) -> Option<&LanguageConfiguration> {
877 pub fn language_config(&self) -> Option<&LanguageConfiguration> {
852 self.language.as_deref()
878 self.language.as_deref()
853 }
879 }
@@ -890,6 +916,7 @@ impl Document {
890 self.path.as_ref()
916 self.path.as_ref()
891 }
917 }
892
918
919+ /// File path as a URL.
893 pub fn url(&self) -> Option<Url> {
920 pub fn url(&self) -> Option<Url> {
894 self.path().map(|path| Url::from_file_path(path).unwrap())
921 self.path().map(|path| Url::from_file_path(path).unwrap())
895 }
922 }