mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 21:28:51 +00:00
use lsp WorkspaceEdit instead of custom source_file_edits and file_system_edits
This commit is contained in:
parent
aea2183799
commit
3ab328b49a
3 changed files with 47 additions and 69 deletions
|
@ -1,11 +1,16 @@
|
||||||
use languageserver_types::{
|
use languageserver_types::{
|
||||||
self, Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
|
self, CreateFile, DocumentChangeOperation, DocumentChanges, InsertTextFormat, Location,
|
||||||
TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat,
|
Position, Range, RenameFile, ResourceOp, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
|
||||||
|
TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier,
|
||||||
|
WorkspaceEdit,
|
||||||
};
|
};
|
||||||
use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition,FileRange, CompletionItem, CompletionItemKind, InsertText, NavigationTarget};
|
use ra_analysis::{
|
||||||
use ra_editor::{LineCol, LineIndex, translate_offset_with_edit};
|
CompletionItem, CompletionItemKind, FileId, FilePosition, FileRange, FileSystemEdit,
|
||||||
use ra_text_edit::{AtomTextEdit, TextEdit};
|
InsertText, NavigationTarget, SourceChange, SourceFileEdit,
|
||||||
|
};
|
||||||
|
use ra_editor::{translate_offset_with_edit, LineCol, LineIndex};
|
||||||
use ra_syntax::{SyntaxKind, TextRange, TextUnit};
|
use ra_syntax::{SyntaxKind, TextRange, TextUnit};
|
||||||
|
use ra_text_edit::{AtomTextEdit, TextEdit};
|
||||||
|
|
||||||
use crate::{req, server_world::ServerWorld, Result};
|
use crate::{req, server_world::ServerWorld, Result};
|
||||||
|
|
||||||
|
@ -49,7 +54,7 @@ impl Conv for CompletionItemKind {
|
||||||
type Output = ::languageserver_types::CompletionItemKind;
|
type Output = ::languageserver_types::CompletionItemKind;
|
||||||
|
|
||||||
fn conv(self) -> <Self as Conv>::Output {
|
fn conv(self) -> <Self as Conv>::Output {
|
||||||
use ::languageserver_types::CompletionItemKind::*;
|
use languageserver_types::CompletionItemKind::*;
|
||||||
match self {
|
match self {
|
||||||
CompletionItemKind::Keyword => Keyword,
|
CompletionItemKind::Keyword => Keyword,
|
||||||
CompletionItemKind::Snippet => Snippet,
|
CompletionItemKind::Snippet => Snippet,
|
||||||
|
@ -266,12 +271,20 @@ impl TryConvWith for SourceChange {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let source_file_edits = self.source_file_edits.try_conv_with(world)?;
|
let mut document_changes: Vec<DocumentChangeOperation> = Vec::new();
|
||||||
let file_system_edits = self.file_system_edits.try_conv_with(world)?;
|
for resource_op in self.file_system_edits.try_conv_with(world)? {
|
||||||
|
document_changes.push(DocumentChangeOperation::Op(resource_op));
|
||||||
|
}
|
||||||
|
for text_document_edit in self.source_file_edits.try_conv_with(world)? {
|
||||||
|
document_changes.push(DocumentChangeOperation::Edit(text_document_edit));
|
||||||
|
}
|
||||||
|
let workspace_edit = WorkspaceEdit {
|
||||||
|
changes: None,
|
||||||
|
document_changes: Some(DocumentChanges::Operations(document_changes)),
|
||||||
|
};
|
||||||
Ok(req::SourceChange {
|
Ok(req::SourceChange {
|
||||||
label: self.label,
|
label: self.label,
|
||||||
source_file_edits,
|
workspace_edit,
|
||||||
file_system_edits,
|
|
||||||
cursor_position,
|
cursor_position,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -301,21 +314,25 @@ impl TryConvWith for SourceFileEdit {
|
||||||
|
|
||||||
impl TryConvWith for FileSystemEdit {
|
impl TryConvWith for FileSystemEdit {
|
||||||
type Ctx = ServerWorld;
|
type Ctx = ServerWorld;
|
||||||
type Output = req::FileSystemEdit;
|
type Output = ResourceOp;
|
||||||
fn try_conv_with(self, world: &ServerWorld) -> Result<req::FileSystemEdit> {
|
fn try_conv_with(self, world: &ServerWorld) -> Result<ResourceOp> {
|
||||||
let res = match self {
|
let res = match self {
|
||||||
FileSystemEdit::CreateFile { source_root, path } => {
|
FileSystemEdit::CreateFile { source_root, path } => {
|
||||||
let uri = world.path_to_uri(source_root, &path)?;
|
let uri = world.path_to_uri(source_root, &path)?.to_string();
|
||||||
req::FileSystemEdit::CreateFile { uri }
|
ResourceOp::Create(CreateFile { uri, options: None })
|
||||||
}
|
}
|
||||||
FileSystemEdit::MoveFile {
|
FileSystemEdit::MoveFile {
|
||||||
src,
|
src,
|
||||||
dst_source_root,
|
dst_source_root,
|
||||||
dst_path,
|
dst_path,
|
||||||
} => {
|
} => {
|
||||||
let src = world.file_id_to_uri(src)?;
|
let old_uri = world.file_id_to_uri(src)?.to_string();
|
||||||
let dst = world.path_to_uri(dst_source_root, &dst_path)?;
|
let new_uri = world.path_to_uri(dst_source_root, &dst_path)?.to_string();
|
||||||
req::FileSystemEdit::MoveFile { src, dst }
|
ResourceOp::Rename(RenameFile {
|
||||||
|
old_uri,
|
||||||
|
new_uri,
|
||||||
|
options: None,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Ok(res)
|
Ok(res)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use serde::{Serialize, Deserialize};
|
|
||||||
use languageserver_types::{Location, Position, Range, TextDocumentIdentifier, Url};
|
use languageserver_types::{Location, Position, Range, TextDocumentIdentifier, Url};
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use url_serde;
|
use url_serde;
|
||||||
|
|
||||||
pub use languageserver_types::{
|
pub use languageserver_types::{
|
||||||
|
@ -8,7 +8,7 @@ pub use languageserver_types::{
|
||||||
CompletionResponse, DocumentOnTypeFormattingParams, DocumentSymbolParams,
|
CompletionResponse, DocumentOnTypeFormattingParams, DocumentSymbolParams,
|
||||||
DocumentSymbolResponse, ExecuteCommandParams, Hover, InitializeResult,
|
DocumentSymbolResponse, ExecuteCommandParams, Hover, InitializeResult,
|
||||||
PublishDiagnosticsParams, ReferenceParams, SignatureHelp, TextDocumentEdit,
|
PublishDiagnosticsParams, ReferenceParams, SignatureHelp, TextDocumentEdit,
|
||||||
TextDocumentPositionParams, TextEdit, WorkspaceSymbolParams,
|
TextDocumentPositionParams, TextEdit, WorkspaceEdit, WorkspaceSymbolParams,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub enum SyntaxTree {}
|
pub enum SyntaxTree {}
|
||||||
|
@ -151,26 +151,10 @@ pub struct Runnable {
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct SourceChange {
|
pub struct SourceChange {
|
||||||
pub label: String,
|
pub label: String,
|
||||||
pub source_file_edits: Vec<TextDocumentEdit>,
|
pub workspace_edit: WorkspaceEdit,
|
||||||
pub file_system_edits: Vec<FileSystemEdit>,
|
|
||||||
pub cursor_position: Option<TextDocumentPositionParams>,
|
pub cursor_position: Option<TextDocumentPositionParams>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
|
||||||
#[serde(tag = "type", rename_all = "camelCase")]
|
|
||||||
pub enum FileSystemEdit {
|
|
||||||
CreateFile {
|
|
||||||
#[serde(with = "url_serde")]
|
|
||||||
uri: Url,
|
|
||||||
},
|
|
||||||
MoveFile {
|
|
||||||
#[serde(with = "url_serde")]
|
|
||||||
src: Url,
|
|
||||||
#[serde(with = "url_serde")]
|
|
||||||
dst: Url,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum InternalFeedback {}
|
pub enum InternalFeedback {}
|
||||||
|
|
||||||
impl Notification for InternalFeedback {
|
impl Notification for InternalFeedback {
|
||||||
|
|
|
@ -3,46 +3,23 @@ import * as lc from 'vscode-languageclient';
|
||||||
|
|
||||||
import { Server } from '../server';
|
import { Server } from '../server';
|
||||||
|
|
||||||
interface FileSystemEdit {
|
|
||||||
type: string;
|
|
||||||
uri?: string;
|
|
||||||
src?: string;
|
|
||||||
dst?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface SourceChange {
|
export interface SourceChange {
|
||||||
label: string;
|
label: string;
|
||||||
sourceFileEdits: lc.TextDocumentEdit[];
|
workspaceEdit: lc.WorkspaceEdit;
|
||||||
fileSystemEdits: FileSystemEdit[];
|
|
||||||
cursorPosition?: lc.TextDocumentPositionParams;
|
cursorPosition?: lc.TextDocumentPositionParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function handle(change: SourceChange) {
|
export async function handle(change: SourceChange) {
|
||||||
const wsEdit = new vscode.WorkspaceEdit();
|
const wsEdit = Server.client.protocol2CodeConverter.asWorkspaceEdit(change.workspaceEdit);
|
||||||
for (const sourceEdit of change.sourceFileEdits) {
|
|
||||||
const uri = Server.client.protocol2CodeConverter.asUri(
|
|
||||||
sourceEdit.textDocument.uri
|
|
||||||
);
|
|
||||||
const edits = Server.client.protocol2CodeConverter.asTextEdits(
|
|
||||||
sourceEdit.edits
|
|
||||||
);
|
|
||||||
wsEdit.set(uri, edits);
|
|
||||||
}
|
|
||||||
let created;
|
let created;
|
||||||
let moved;
|
let moved;
|
||||||
for (const fsEdit of change.fileSystemEdits) {
|
if (change.workspaceEdit.documentChanges) {
|
||||||
switch (fsEdit.type) {
|
for (const docChange of change.workspaceEdit.documentChanges) {
|
||||||
case 'createFile':
|
if (lc.CreateFile.is(docChange)) {
|
||||||
const uri = vscode.Uri.parse(fsEdit.uri!);
|
created = docChange.uri;
|
||||||
wsEdit.createFile(uri);
|
} else if (lc.RenameFile.is(docChange)) {
|
||||||
created = uri;
|
moved = docChange.newUri;
|
||||||
break;
|
}
|
||||||
case 'moveFile':
|
|
||||||
const src = vscode.Uri.parse(fsEdit.src!);
|
|
||||||
const dst = vscode.Uri.parse(fsEdit.dst!);
|
|
||||||
wsEdit.renameFile(src, dst);
|
|
||||||
moved = dst;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const toOpen = created || moved;
|
const toOpen = created || moved;
|
||||||
|
@ -65,6 +42,6 @@ export async function handle(change: SourceChange) {
|
||||||
if (!editor.selection.isEmpty) {
|
if (!editor.selection.isEmpty) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
editor!.selection = new vscode.Selection(position, position);
|
editor.selection = new vscode.Selection(position, position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue