diff options
author | Aleksey Kladov <[email protected]> | 2018-08-15 15:24:20 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-15 15:24:20 +0100 |
commit | 9f6cf42c5fd0bd98dd3445239f2c6414e8fd9324 (patch) | |
tree | 5e6496a681a56e9fd1b6e6bae196538cd471e2d0 /crates/server/src/conv.rs | |
parent | 109658332a75ca91d6dc2bf573e0ab77fa5619ca (diff) |
Switch to file ids
Diffstat (limited to 'crates/server/src/conv.rs')
-rw-r--r-- | crates/server/src/conv.rs | 72 |
1 files changed, 59 insertions, 13 deletions
diff --git a/crates/server/src/conv.rs b/crates/server/src/conv.rs index 3ddce5fb3..bbe512ece 100644 --- a/crates/server/src/conv.rs +++ b/crates/server/src/conv.rs | |||
@@ -1,10 +1,12 @@ | |||
1 | use std::path::Path; | 1 | use languageserver_types::{ |
2 | 2 | Range, SymbolKind, Position, TextEdit, Location, Url, | |
3 | use languageserver_types::{Range, SymbolKind, Position, TextEdit, Location, Url}; | 3 | TextDocumentIdentifier, VersionedTextDocumentIdentifier, TextDocumentItem, |
4 | }; | ||
4 | use libeditor::{LineIndex, LineCol, Edit, AtomEdit}; | 5 | use libeditor::{LineIndex, LineCol, Edit, AtomEdit}; |
5 | use libsyntax2::{SyntaxKind, TextUnit, TextRange}; | 6 | use libsyntax2::{SyntaxKind, TextUnit, TextRange}; |
7 | use libanalysis::FileId; | ||
6 | 8 | ||
7 | use Result; | 9 | use {Result, PathMap}; |
8 | 10 | ||
9 | pub trait Conv { | 11 | pub trait Conv { |
10 | type Output; | 12 | type Output; |
@@ -115,21 +117,65 @@ impl ConvWith for AtomEdit { | |||
115 | } | 117 | } |
116 | } | 118 | } |
117 | 119 | ||
118 | impl<'a> TryConvWith for (&'a Path, TextRange) { | 120 | impl<'a> TryConvWith for &'a Url { |
119 | type Ctx = LineIndex; | 121 | type Ctx = PathMap; |
120 | type Output = Location; | 122 | type Output = FileId; |
123 | fn try_conv_with(self, path_map: &PathMap) -> Result<FileId> { | ||
124 | let path = self.to_file_path() | ||
125 | .map_err(|()| format_err!("invalid uri: {}", self))?; | ||
126 | path_map.get_id(&path).ok_or_else(|| format_err!("unknown file: {}", path.display())) | ||
127 | } | ||
128 | } | ||
129 | |||
130 | impl TryConvWith for FileId { | ||
131 | type Ctx = PathMap; | ||
132 | type Output = Url; | ||
133 | fn try_conv_with(self, path_map: &PathMap) -> Result<Url> { | ||
134 | let path = path_map.get_path(self); | ||
135 | let url = Url::from_file_path(path) | ||
136 | .map_err(|()| format_err!("can't convert path to url: {}", path.display()))?; | ||
137 | Ok(url) | ||
138 | } | ||
139 | } | ||
140 | |||
141 | impl<'a> TryConvWith for &'a TextDocumentItem { | ||
142 | type Ctx = PathMap; | ||
143 | type Output = FileId; | ||
144 | fn try_conv_with(self, path_map: &PathMap) -> Result<FileId> { | ||
145 | self.uri.try_conv_with(path_map) | ||
146 | } | ||
147 | } | ||
121 | 148 | ||
122 | fn try_conv_with(self, line_index: &LineIndex) -> Result<Location> { | 149 | impl<'a> TryConvWith for &'a VersionedTextDocumentIdentifier { |
150 | type Ctx = PathMap; | ||
151 | type Output = FileId; | ||
152 | fn try_conv_with(self, path_map: &PathMap) -> Result<FileId> { | ||
153 | self.uri.try_conv_with(path_map) | ||
154 | } | ||
155 | } | ||
156 | |||
157 | impl<'a> TryConvWith for &'a TextDocumentIdentifier { | ||
158 | type Ctx = PathMap; | ||
159 | type Output = FileId; | ||
160 | fn try_conv_with(self, path_map: &PathMap) -> Result<FileId> { | ||
161 | self.uri.try_conv_with(path_map) | ||
162 | } | ||
163 | } | ||
164 | |||
165 | pub fn to_location( | ||
166 | file_id: FileId, | ||
167 | range: TextRange, | ||
168 | path_map: &PathMap, | ||
169 | line_index: &LineIndex, | ||
170 | ) -> Result<Location> { | ||
171 | let url = file_id.try_conv_with(path_map)?; | ||
123 | let loc = Location::new( | 172 | let loc = Location::new( |
124 | Url::from_file_path(self.0) | 173 | url, |
125 | .map_err(|()| format_err!("can't convert path to url: {}", self.0.display()))?, | 174 | range.conv_with(line_index), |
126 | self.1.conv_with(line_index), | ||
127 | ); | 175 | ); |
128 | Ok(loc) | 176 | Ok(loc) |
129 | } | ||
130 | } | 177 | } |
131 | 178 | ||
132 | |||
133 | pub trait MapConvWith<'a>: Sized { | 179 | pub trait MapConvWith<'a>: Sized { |
134 | type Ctx; | 180 | type Ctx; |
135 | type Output; | 181 | type Output; |