translate \n -> \r\n on the way out

This commit is contained in:
Aleksey Kladov 2019-08-20 18:53:59 +03:00
parent 80a6e61446
commit 6ea4184fd1
7 changed files with 50 additions and 33 deletions

8
Cargo.lock generated
View File

@ -1092,7 +1092,7 @@ dependencies = [
"ra_hir 0.1.0", "ra_hir 0.1.0",
"ra_ide_api 0.1.0", "ra_ide_api 0.1.0",
"ra_project_model 0.1.0", "ra_project_model 0.1.0",
"ra_vfs 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "ra_vfs 0.2.6",
"ra_vfs_glob 0.1.0", "ra_vfs_glob 0.1.0",
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -1198,7 +1198,7 @@ dependencies = [
"ra_project_model 0.1.0", "ra_project_model 0.1.0",
"ra_syntax 0.1.0", "ra_syntax 0.1.0",
"ra_text_edit 0.1.0", "ra_text_edit 0.1.0",
"ra_vfs 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "ra_vfs 0.2.6",
"ra_vfs_glob 0.1.0", "ra_vfs_glob 0.1.0",
"relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1308,7 +1308,6 @@ dependencies = [
[[package]] [[package]]
name = "ra_vfs" name = "ra_vfs"
version = "0.2.6" version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"crossbeam-channel 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-channel 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1324,7 +1323,7 @@ name = "ra_vfs_glob"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"globset 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "globset 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"ra_vfs 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "ra_vfs 0.2.6",
] ]
[[package]] [[package]]
@ -2135,7 +2134,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" "checksum quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1"
"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
"checksum ra_rustc_lexer 0.1.0-pre.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6baccda91574dfadd7f8a0bc8f9f110f874b6b484289b2536d3dbf4f0d5d97bb" "checksum ra_rustc_lexer 0.1.0-pre.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6baccda91574dfadd7f8a0bc8f9f110f874b6b484289b2536d3dbf4f0d5d97bb"
"checksum ra_vfs 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "865bb9e0f71916f7c7527af4843a2a67d1b0789f7c91c512a6b4ded69af98249"
"checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" "checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca"
"checksum rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d47eab0e83d9693d40f825f86948aa16eff6750ead4bdffc4ab95b8b3a7f052c" "checksum rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d47eab0e83d9693d40f825f86948aa16eff6750ead4bdffc4ab95b8b3a7f052c"
"checksum rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" "checksum rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef"

View File

@ -6,3 +6,4 @@ incremental = true
debug = 1 # only line info debug = 1 # only line info
[patch.'crates-io'] [patch.'crates-io']
ra_vfs = { path = "../ra_vfs" }

View File

@ -11,6 +11,7 @@
}; };
use ra_syntax::{SyntaxKind, TextRange, TextUnit}; use ra_syntax::{SyntaxKind, TextRange, TextUnit};
use ra_text_edit::{AtomTextEdit, TextEdit}; use ra_text_edit::{AtomTextEdit, TextEdit};
use ra_vfs::LineEndings;
use crate::{req, world::WorldSnapshot, Result}; use crate::{req, world::WorldSnapshot, Result};
@ -88,10 +89,10 @@ fn conv(self) -> DiagnosticSeverity {
} }
} }
impl ConvWith<&'_ LineIndex> for CompletionItem { impl ConvWith<(&'_ LineIndex, LineEndings)> for CompletionItem {
type Output = ::lsp_types::CompletionItem; type Output = ::lsp_types::CompletionItem;
fn conv_with(self, ctx: &LineIndex) -> ::lsp_types::CompletionItem { fn conv_with(self, ctx: (&LineIndex, LineEndings)) -> ::lsp_types::CompletionItem {
let mut additional_text_edits = Vec::new(); let mut additional_text_edits = Vec::new();
let mut text_edit = None; let mut text_edit = None;
// LSP does not allow arbitrary edits in completion, so we have to do a // LSP does not allow arbitrary edits in completion, so we have to do a
@ -202,22 +203,27 @@ fn conv(self) -> Self::Output {
} }
} }
impl ConvWith<&'_ LineIndex> for TextEdit { impl ConvWith<(&'_ LineIndex, LineEndings)> for TextEdit {
type Output = Vec<lsp_types::TextEdit>; type Output = Vec<lsp_types::TextEdit>;
fn conv_with(self, line_index: &LineIndex) -> Vec<lsp_types::TextEdit> { fn conv_with(self, ctx: (&LineIndex, LineEndings)) -> Vec<lsp_types::TextEdit> {
self.as_atoms().iter().map_conv_with(line_index).collect() self.as_atoms().iter().map_conv_with(ctx).collect()
} }
} }
impl ConvWith<&'_ LineIndex> for &'_ AtomTextEdit { impl ConvWith<(&'_ LineIndex, LineEndings)> for &'_ AtomTextEdit {
type Output = lsp_types::TextEdit; type Output = lsp_types::TextEdit;
fn conv_with(self, line_index: &LineIndex) -> lsp_types::TextEdit { fn conv_with(
lsp_types::TextEdit { self,
range: self.delete.conv_with(line_index), (line_index, line_endings): (&LineIndex, LineEndings),
new_text: self.insert.clone(), ) -> lsp_types::TextEdit {
eprintln!("line_endings = {:?}", line_endings);
let mut new_text = self.insert.clone();
if line_endings == LineEndings::Dos {
new_text = new_text.replace('\n', "\r\n");
} }
lsp_types::TextEdit { range: self.delete.conv_with(line_index), new_text }
} }
} }
@ -352,7 +358,9 @@ fn try_conv_with(self, world: &WorldSnapshot) -> Result<TextDocumentEdit> {
version: None, version: None,
}; };
let line_index = world.analysis().file_line_index(self.file_id)?; let line_index = world.analysis().file_line_index(self.file_id)?;
let edits = self.edit.as_atoms().iter().map_conv_with(&line_index).collect(); let line_endings = world.file_line_endings(self.file_id);
let edits =
self.edit.as_atoms().iter().map_conv_with((&line_index, line_endings)).collect();
Ok(TextDocumentEdit { text_document, edits }) Ok(TextDocumentEdit { text_document, edits })
} }
} }

View File

@ -138,6 +138,7 @@ pub fn handle_on_type_formatting(
let _p = profile("handle_on_type_formatting"); let _p = profile("handle_on_type_formatting");
let mut position = params.text_document_position.try_conv_with(&world)?; let mut position = params.text_document_position.try_conv_with(&world)?;
let line_index = world.analysis().file_line_index(position.file_id)?; let line_index = world.analysis().file_line_index(position.file_id)?;
let line_endings = world.file_line_endings(position.file_id);
// in `ra_ide_api`, the `on_type` invariant is that // in `ra_ide_api`, the `on_type` invariant is that
// `text.char_at(position) == typed_char`. // `text.char_at(position) == typed_char`.
@ -156,7 +157,7 @@ pub fn handle_on_type_formatting(
// This should be a single-file edit // This should be a single-file edit
let edit = edit.source_file_edits.pop().unwrap(); let edit = edit.source_file_edits.pop().unwrap();
let change: Vec<TextEdit> = edit.edit.conv_with(&line_index); let change: Vec<TextEdit> = edit.edit.conv_with((&line_index, line_endings));
Ok(Some(change)) Ok(Some(change))
} }
@ -370,8 +371,9 @@ pub fn handle_completion(
Some(items) => items, Some(items) => items,
}; };
let line_index = world.analysis().file_line_index(position.file_id)?; let line_index = world.analysis().file_line_index(position.file_id)?;
let line_endings = world.file_line_endings(position.file_id);
let items: Vec<CompletionItem> = let items: Vec<CompletionItem> =
items.into_iter().map(|item| item.conv_with(&line_index)).collect(); items.into_iter().map(|item| item.conv_with((&line_index, line_endings))).collect();
Ok(Some(items.into())) Ok(Some(items.into()))
} }

View File

@ -9,7 +9,7 @@
use ra_ide_api::{ use ra_ide_api::{
Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId, Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId,
}; };
use ra_vfs::{RootEntry, Vfs, VfsChange, VfsFile, VfsRoot}; use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot};
use ra_vfs_glob::{Glob, RustPackageFilterBuilder}; use ra_vfs_glob::{Glob, RustPackageFilterBuilder};
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
@ -210,6 +210,10 @@ pub fn file_id_to_uri(&self, id: FileId) -> Result<Url> {
Ok(url) Ok(url)
} }
pub fn file_line_endings(&self, id: FileId) -> LineEndings {
self.vfs.read().file_line_endings(VfsFile(id.0))
}
pub fn path_to_uri(&self, root: SourceRootId, path: &RelativePathBuf) -> Result<Url> { pub fn path_to_uri(&self, root: SourceRootId, path: &RelativePathBuf) -> Result<Url> {
let base = self.vfs.read().root2path(VfsRoot(root.0)); let base = self.vfs.read().root2path(VfsRoot(root.0));
let path = path.to_path(base); let path = path.to_path(base);

View File

@ -208,7 +208,7 @@ fn main() {}
"range": { "range": {
"end": { "end": {
"character": 0, "character": 0,
"line": 6 "line": 7
}, },
"start": { "start": {
"character": 0, "character": 0,
@ -418,15 +418,15 @@ fn main() {{}}
#[test] #[test]
fn preserves_dos_line_endings() { fn preserves_dos_line_endings() {
let server = Project::with_fixture( let server = Project::with_fixture(
&r#" &"
//- Cargo.toml //- Cargo.toml
[package] [package]
name = "foo" name = \"foo\"
version = "0.0.0" version = \"0.0.0\"
//- src/main.rs //- src/main.rs
/// Some Docs\r\nfn main() {} /// Some Docs\r\nfn main() {}
"#, ",
) )
.server(); .server();

View File

@ -134,13 +134,17 @@ macro_rules! flush {
} }
}; };
}; };
let margin = fixture let margin = fixture
.lines() .lines()
.filter(|it| it.trim_start().starts_with("//-")) .filter(|it| it.trim_start().starts_with("//-"))
.map(|it| it.len() - it.trim_start().len()) .map(|it| it.len() - it.trim_start().len())
.next() .next()
.expect("empty fixture"); .expect("empty fixture");
let lines = fixture.lines().filter_map(|line| {
let lines = fixture
.split('\n') // don't use `.lines` to not drop `\r\n`
.filter_map(|line| {
if line.len() >= margin { if line.len() >= margin {
assert!(line[..margin].trim().is_empty()); assert!(line[..margin].trim().is_empty());
Some(&line[margin..]) Some(&line[margin..])