]> git.lizzy.rs Git - rust.git/blobdiff - crates/rust-analyzer/src/conv.rs
Merge #4161
[rust.git] / crates / rust-analyzer / src / conv.rs
index 7e30956cc30302b504e5fa85db764b04ad793a43..7be5ebcdb5a0d13599cc5a8d628c48186cc46936 100644 (file)
@@ -9,12 +9,12 @@
     TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, WorkspaceEdit,
 };
 use ra_ide::{
-    translate_offset_with_edit, CompletionItem, CompletionItemKind, CompletionScore, FileId,
-    FilePosition, FileRange, FileSystemEdit, Fold, FoldKind, Highlight, HighlightModifier,
-    HighlightTag, InlayHint, InlayKind, InsertTextFormat, LineCol, LineIndex, NavigationTarget,
-    RangeInfo, ReferenceAccess, Severity, SourceChange, SourceFileEdit,
+    translate_offset_with_edit, CompletionItem, CompletionItemKind, FileId, FilePosition,
+    FileRange, FileSystemEdit, Fold, FoldKind, Highlight, HighlightModifier, HighlightTag,
+    InlayHint, InlayKind, InsertTextFormat, LineCol, LineIndex, NavigationTarget, RangeInfo,
+    ReferenceAccess, Severity, SourceChange, SourceFileEdit,
 };
-use ra_syntax::{SyntaxKind, TextRange, TextUnit};
+use ra_syntax::{SyntaxKind, TextRange, TextSize};
 use ra_text_edit::{AtomTextEdit, TextEdit};
 use ra_vfs::LineEndings;
 
@@ -25,7 +25,8 @@
     Result,
 };
 use semantic_tokens::{
-    ATTRIBUTE, BUILTIN_TYPE, ENUM_MEMBER, LIFETIME, TYPE_ALIAS, UNION, UNRESOLVED_REFERENCE,
+    ATTRIBUTE, BUILTIN_TYPE, ENUM_MEMBER, FORMAT_SPECIFIER, LIFETIME, TYPE_ALIAS, UNION,
+    UNRESOLVED_REFERENCE,
 };
 
 pub trait Conv {
@@ -100,6 +101,7 @@ fn conv(self) -> <Self as Conv>::Output {
             CompletionItemKind::Method => Method,
             CompletionItemKind::TypeParam => TypeParameter,
             CompletionItemKind::Macro => Method,
+            CompletionItemKind::Attribute => EnumMember,
         }
     }
 }
@@ -114,22 +116,22 @@ fn conv(self) -> DiagnosticSeverity {
     }
 }
 
-impl ConvWith<(&LineIndex, LineEndings, &mut usize)> for CompletionItem {
+impl ConvWith<(&LineIndex, LineEndings)> for CompletionItem {
     type Output = ::lsp_types::CompletionItem;
 
-    fn conv_with(self, ctx: (&LineIndex, LineEndings, &mut usize)) -> ::lsp_types::CompletionItem {
+    fn conv_with(self, ctx: (&LineIndex, LineEndings)) -> ::lsp_types::CompletionItem {
         let mut additional_text_edits = Vec::new();
         let mut text_edit = None;
         // LSP does not allow arbitrary edits in completion, so we have to do a
         // non-trivial mapping here.
         for atom_edit in self.text_edit().as_atoms() {
-            if self.source_range().is_subrange(&atom_edit.delete) {
+            if atom_edit.delete.contains_range(self.source_range()) {
                 text_edit = Some(if atom_edit.delete == self.source_range() {
                     atom_edit.conv_with((ctx.0, ctx.1))
                 } else {
                     assert!(self.source_range().end() == atom_edit.delete.end());
                     let range1 =
-                        TextRange::from_to(atom_edit.delete.start(), self.source_range().start());
+                        TextRange::new(atom_edit.delete.start(), self.source_range().start());
                     let range2 = self.source_range();
                     let edit1 = AtomTextEdit::replace(range1, String::new());
                     let edit2 = AtomTextEdit::replace(range2, atom_edit.insert.clone());
@@ -137,7 +139,7 @@ fn conv_with(self, ctx: (&LineIndex, LineEndings, &mut usize)) -> ::lsp_types::C
                     edit2.conv_with((ctx.0, ctx.1))
                 })
             } else {
-                assert!(self.source_range().intersection(&atom_edit.delete).is_none());
+                assert!(self.source_range().intersect(atom_edit.delete).is_none());
                 additional_text_edits.push(atom_edit.conv_with((ctx.0, ctx.1)));
             }
         }
@@ -148,7 +150,7 @@ fn conv_with(self, ctx: (&LineIndex, LineEndings, &mut usize)) -> ::lsp_types::C
             detail: self.detail().map(|it| it.to_string()),
             filter_text: Some(self.lookup().to_string()),
             kind: self.kind().map(|it| it.conv()),
-            text_edit: Some(text_edit),
+            text_edit: Some(text_edit.into()),
             additional_text_edits: Some(additional_text_edits),
             documentation: self.documentation().map(|it| it.conv()),
             deprecated: Some(self.deprecated()),
@@ -165,13 +167,8 @@ fn conv_with(self, ctx: (&LineIndex, LineEndings, &mut usize)) -> ::lsp_types::C
             ..Default::default()
         };
 
-        if let Some(score) = self.score() {
-            match score {
-                CompletionScore::TypeAndNameMatch => res.preselect = Some(true),
-                CompletionScore::TypeMatch => {}
-            }
-            res.sort_text = Some(format!("{:02}", *ctx.2));
-            *ctx.2 += 1;
+        if self.score().is_some() {
+            res.preselect = Some(true)
         }
 
         if self.deprecated() {
@@ -188,15 +185,15 @@ fn conv_with(self, ctx: (&LineIndex, LineEndings, &mut usize)) -> ::lsp_types::C
 }
 
 impl ConvWith<&LineIndex> for Position {
-    type Output = TextUnit;
+    type Output = TextSize;
 
-    fn conv_with(self, line_index: &LineIndex) -> TextUnit {
+    fn conv_with(self, line_index: &LineIndex) -> TextSize {
         let line_col = LineCol { line: self.line as u32, col_utf16: self.character as u32 };
         line_index.offset(line_col)
     }
 }
 
-impl ConvWith<&LineIndex> for TextUnit {
+impl ConvWith<&LineIndex> for TextSize {
     type Output = Position;
 
     fn conv_with(self, line_index: &LineIndex) -> Position {
@@ -217,7 +214,7 @@ impl ConvWith<&LineIndex> for Range {
     type Output = TextRange;
 
     fn conv_with(self, line_index: &LineIndex) -> TextRange {
-        TextRange::from_to(self.start.conv_with(line_index), self.end.conv_with(line_index))
+        TextRange::new(self.start.conv_with(line_index), self.end.conv_with(line_index))
     }
 }
 
@@ -304,7 +301,7 @@ fn conv_with(self, ctx: &FoldConvCtx) -> lsp_types::FoldingRange {
             // range.end.line from the folding region if there is more text after range.end
             // on the same line.
             let has_more_text_on_end_line = ctx.text
-                [TextRange::from_to(self.range.end(), TextUnit::of_str(ctx.text))]
+                [TextRange::new(self.range.end(), TextSize::of(ctx.text))]
             .chars()
             .take_while(|it| *it != '\n')
             .any(|it| !it.is_whitespace());
@@ -385,6 +382,7 @@ fn conv(self) -> Self::Output {
             HighlightTag::Attribute => ATTRIBUTE,
             HighlightTag::Keyword => SemanticTokenType::KEYWORD,
             HighlightTag::UnresolvedReference => UNRESOLVED_REFERENCE,
+            HighlightTag::FormatSpecifier => FORMAT_SPECIFIER,
         };
 
         for modifier in self.modifiers.iter() {