Repository: rust-analyzer/rust-analyzer
Pull request #417 (2019-01-03 16:10:55)

travis-compile


Patch:
diff --git a/.travis.yml b/.travis.yml
index 04bb3be370..86bce93d62 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -10,6 +10,7 @@ build: &rust_build
     script:
         - cargo gen-tests --verify
         - cargo gen-syntax --verify
+        - cargo test --no-run  # let's measure compile time separately
         - cargo test
     env:
         - RUSTFLAGS="-D warnings", CARGO_INCREMENTAL=0
@@ -25,7 +26,7 @@ matrix:
               - cd editors/code && npm ci && npm run travis
 
         - os: windows
-          if: branch = master
+          if: branch = master AND type = push
           before_script:
               - dos2unix ./crates/ra_syntax/tests/data/parser/**/*.txt
               - dos2unix ./crates/ra_syntax/tests/data/parser/**/*.rs
diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs
index 7230fb101b..1107ffc8b9 100644
--- a/crates/ra_lsp_server/src/conv.rs
+++ b/crates/ra_lsp_server/src/conv.rs
@@ -1,16 +1,11 @@
 use languageserver_types::{
-    self, CreateFile, DocumentChangeOperation, DocumentChanges, InsertTextFormat, Location,
-    Position, Range, RenameFile, ResourceOp, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
-    TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier,
-    WorkspaceEdit,
+    self, Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
+    TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat,
 };
-use ra_analysis::{
-    CompletionItem, CompletionItemKind, FileId, FilePosition, FileRange, FileSystemEdit,
-    InsertText, NavigationTarget, SourceChange, SourceFileEdit,
-};
-use ra_editor::{translate_offset_with_edit, LineCol, LineIndex};
-use ra_syntax::{SyntaxKind, TextRange, TextUnit};
+use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition,FileRange,  CompletionItem, CompletionItemKind, InsertText, NavigationTarget};
+use ra_editor::{LineCol, LineIndex, translate_offset_with_edit};
 use ra_text_edit::{AtomTextEdit, TextEdit};
+use ra_syntax::{SyntaxKind, TextRange, TextUnit};
 
 use crate::{req, server_world::ServerWorld, Result};
 
@@ -54,7 +49,7 @@ impl Conv for CompletionItemKind {
     type Output = ::languageserver_types::CompletionItemKind;
 
     fn conv(self) -> <Self as Conv>::Output {
-        use languageserver_types::CompletionItemKind::*;
+        use ::languageserver_types::CompletionItemKind::*;
         match self {
             CompletionItemKind::Keyword => Keyword,
             CompletionItemKind::Snippet => Snippet,
@@ -271,20 +266,12 @@ impl TryConvWith for SourceChange {
                 })
             }
         };
-        let mut document_changes: Vec<DocumentChangeOperation> = Vec::new();
-        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)),
-        };
+        let source_file_edits = self.source_file_edits.try_conv_with(world)?;
+        let file_system_edits = self.file_system_edits.try_conv_with(world)?;
         Ok(req::SourceChange {
             label: self.label,
-            workspace_edit,
+            source_file_edits,
+            file_system_edits,
             cursor_position,
         })
     }
@@ -314,25 +301,21 @@ impl TryConvWith for SourceFileEdit {
 
 impl TryConvWith for FileSystemEdit {
     type Ctx = ServerWorld;
-    type Output = ResourceOp;
-    fn try_conv_with(self, world: &ServerWorld) -> Result<ResourceOp> {
+    type Output = req::FileSystemEdit;
+    fn try_conv_with(self, world: &ServerWorld) -> Result<req::FileSystemEdit> {
         let res = match self {
             FileSystemEdit::CreateFile { source_root, path } => {
-                let uri = world.path_to_uri(source_root, &path)?.to_string();
-                ResourceOp::Create(CreateFile { uri, options: None })
+                let uri = world.path_to_uri(source_root, &path)?;
+                req::FileSystemEdit::CreateFile { uri }
             }
             FileSystemEdit::MoveFile {
                 src,
                 dst_source_root,
                 dst_path,
             } => {
-                let old_uri = world.file_id_to_uri(src)?.to_string();
-                let new_uri = world.path_to_uri(dst_source_root, &dst_path)?.to_string();
-                ResourceOp::Rename(RenameFile {
-                    old_uri,
-                    new_uri,
-                    options: None,
-                })
+                let src = world.file_id_to_uri(src)?;
+                let dst = world.path_to_uri(dst_source_root, &dst_path)?;
+                req::FileSystemEdit::MoveFile { src, dst }
             }
         };
         Ok(res)
diff --git a/crates/ra_lsp_server/src/req.rs b/crates/ra_lsp_server/src/req.rs
index b41e903280..747ab8a8c9 100644
--- a/crates/ra_lsp_server/src/req.rs
+++ b/crates/ra_lsp_server/src/req.rs
@@ -1,6 +1,6 @@
+use serde::{Serialize, Deserialize};
 use languageserver_types::{Location, Position, Range, TextDocumentIdentifier, Url};
 use rustc_hash::FxHashMap;
-use serde::{Deserialize, Serialize};
 use url_serde;
 
 pub use languageserver_types::{
@@ -8,7 +8,7 @@ pub use languageserver_types::{
     CompletionResponse, DocumentOnTypeFormattingParams, DocumentSymbolParams,
     DocumentSymbolResponse, ExecuteCommandParams, Hover, InitializeResult,
     PublishDiagnosticsParams, ReferenceParams, SignatureHelp, TextDocumentEdit,
-    TextDocumentPositionParams, TextEdit, WorkspaceEdit, WorkspaceSymbolParams,
+    TextDocumentPositionParams, TextEdit, WorkspaceSymbolParams,
 };
 
 pub enum SyntaxTree {}
@@ -151,10 +151,26 @@ pub struct Runnable {
 #[serde(rename_all = "camelCase")]
 pub struct SourceChange {
     pub label: String,
-    pub workspace_edit: WorkspaceEdit,
+    pub source_file_edits: Vec<TextDocumentEdit>,
+    pub file_system_edits: Vec<FileSystemEdit>,
     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 {}
 
 impl Notification for InternalFeedback {
diff --git a/editors/code/src/commands/apply_source_change.ts b/editors/code/src/commands/apply_source_change.ts
index 10dbf72c07..cf921e3ac0 100644
--- a/editors/code/src/commands/apply_source_change.ts
+++ b/editors/code/src/commands/apply_source_change.ts
@@ -3,23 +3,46 @@ import * as lc from 'vscode-languageclient';
 
 import { Server } from '../server';
 
+interface FileSystemEdit {
+    type: string;
+    uri?: string;
+    src?: string;
+    dst?: string;
+}
+
 export interface SourceChange {
     label: string;
-    workspaceEdit: lc.WorkspaceEdit;
+    sourceFileEdits: lc.TextDocumentEdit[];
+    fileSystemEdits: FileSystemEdit[];
     cursorPosition?: lc.TextDocumentPositionParams;
 }
 
 export async function handle(change: SourceChange) {
-    const wsEdit = Server.client.protocol2CodeConverter.asWorkspaceEdit(change.workspaceEdit);
+    const wsEdit = new vscode.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 moved;
-    if (change.workspaceEdit.documentChanges) {
-        for (const docChange of change.workspaceEdit.documentChanges) {
-            if (lc.CreateFile.is(docChange)) {
-                created = docChange.uri;
-            } else if (lc.RenameFile.is(docChange)) {
-                moved = docChange.newUri;
-            }
+    for (const fsEdit of change.fileSystemEdits) {
+        switch (fsEdit.type) {
+            case 'createFile':
+                const uri = vscode.Uri.parse(fsEdit.uri!);
+                wsEdit.createFile(uri);
+                created = uri;
+                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;
@@ -42,6 +65,6 @@ export async function handle(change: SourceChange) {
         if (!editor.selection.isEmpty) {
             return;
         }
-        editor.selection = new vscode.Selection(position, position);
+        editor!.selection = new vscode.Selection(position, position);
     }
 }


Test patch:
diff --git a/crates/ra_lsp_server/tests/heavy_tests/main.rs b/crates/ra_lsp_server/tests/heavy_tests/main.rs
index 4cae44eab2..b0e1e65b62 100644
--- a/crates/ra_lsp_server/tests/heavy_tests/main.rs
+++ b/crates/ra_lsp_server/tests/heavy_tests/main.rs
@@ -1,12 +1,8 @@
 mod support;
 
-use languageserver_types::{
-    CodeActionContext, DocumentFormattingParams, FormattingOptions, Position, Range,
-};
-use ra_lsp_server::req::{
-    CodeActionParams, CodeActionRequest, Formatting, Runnables, RunnablesParams,
-};
 use serde_json::json;
+use ra_lsp_server::req::{Runnables, RunnablesParams, CodeActionRequest, CodeActionParams, Formatting};
+use languageserver_types::{Position, Range, CodeActionContext, DocumentFormattingParams, FormattingOptions};
 
 use crate::support::project;
 
@@ -207,15 +203,14 @@ fn main() {}
               "arguments": [
                 {
                   "cursorPosition": null,
-                  "workspaceEdit": {
-                    "documentChanges": [
-                      {
-                        "kind": "create",
+                  "fileSystemEdits": [
+                    {
+                        "type": "createFile",
                         "uri": "file:///[..]/src/bar.rs"
-                      }
-                    ]
-                  },
-                  "label": "create module"
+                    }
+                  ],
+                  "label": "create module",
+                  "sourceFileEdits": []
                 }
               ],
               "command": "ra-lsp.applySourceChange",