Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Features/LanguageServer/Protocol/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ public static Uri GetUriForRenamedDocument(this TextDocument document)
return ProtocolConversions.GetUriFromFilePath(path);
}

/// <summary>
/// Generate the Uri of a document based on the name and the project path of the document.
/// </summary>
public static Uri GetUriFromProjectPath(this TextDocument document)
{
Contract.ThrowIfNull(document.Name);
Contract.ThrowIfNull(document.Project.FilePath);
var directoryName = Path.GetDirectoryName(document.Project.FilePath);
Contract.ThrowIfNull(directoryName);

var path = Path.Combine(directoryName, document.Name);
return ProtocolConversions.GetUriFromFilePath(path);
}

public static Uri? TryGetURI(this TextDocument document, RequestContext? context = null)
=> ProtocolConversions.TryGetUriFromFilePath(document.FilePath, context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,17 @@ async Task AddTextDocumentAdditionsAsync<TTextDocument>(
var newTextDoc = getNewDocument(docId);
Contract.ThrowIfNull(newTextDoc);

// Create the document as empty
textDocumentEdits.Add(new CreateFile { Uri = newTextDoc.GetURI() });
// If the file path doesn't exist, try to create from project path
var uri = newTextDoc.FilePath != null
? newTextDoc.GetURI()
: newTextDoc.GetUriFromProjectPath();
textDocumentEdits.Add(new CreateFile { Uri = uri });

// And then give it content
var newText = await newTextDoc.GetTextAsync(cancellationToken).ConfigureAwait(false);
var emptyDocumentRange = new LSP.Range { Start = new Position { Line = 0, Character = 0 }, End = new Position { Line = 0, Character = 0 } };
var edit = new TextEdit { Range = emptyDocumentRange, NewText = newText.ToString() };
var documentIdentifier = new OptionalVersionedTextDocumentIdentifier { Uri = newTextDoc.GetURI() };
var documentIdentifier = new OptionalVersionedTextDocumentIdentifier { Uri = uri };
textDocumentEdits.Add(new TextDocumentEdit { TextDocument = documentIdentifier, Edits = new[] { edit } });
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Roslyn.Test.Utilities;
using Xunit;
Expand Down Expand Up @@ -297,6 +299,127 @@ public async Task TestLinkedDocuments(bool mutatingLspWorkspace)
AssertJsonEquals(expectedCodeAction, actualResolvedAction);
}

[WpfTheory, CombinatorialData]
public async Task TestMoveTypeToDifferentFile(bool mutatingLspWorkspace)
{
var markUp = @"
class {|caret:ABC|}
{
}
class BCD
{
}";

await using var testLspServer = await CreateTestLspServerAsync(markUp, mutatingLspWorkspace, new InitializationOptions
{
ClientCapabilities = new ClientCapabilities()
{
Workspace = new WorkspaceClientCapabilities
{
WorkspaceEdit = new WorkspaceEditSetting
{
ResourceOperations = new ResourceOperationKind[] { ResourceOperationKind.Create }
}
}
}
});

var unresolvedCodeAction = CodeActionsTests.CreateCodeAction(
title: string.Format(FeaturesResources.Move_type_to_0, "ABC.cs"),
kind: CodeActionKind.Refactor,
children: Array.Empty<LSP.VSInternalCodeAction>(),
data: CreateCodeActionResolveData(
string.Format(FeaturesResources.Move_type_to_0, "ABC.cs"),
testLspServer.GetLocations("caret").Single()),
priority: VSInternalPriorityLevel.Normal,
groupName: "Roslyn2",
applicableRange: new LSP.Range { Start = new Position { Line = 0, Character = 6 }, End = new Position { Line = 0, Character = 9 } },
diagnostics: null);

var testWorkspace = testLspServer.TestWorkspace;
var actualResolvedAction = await RunGetCodeActionResolveAsync(testLspServer, unresolvedCodeAction);

var project = testWorkspace.CurrentSolution.Projects.Single();
var newDocumentUri = ProtocolConversions.GetUriFromFilePath(Path.Combine(Path.GetDirectoryName(project.FilePath), "ABC.cs"));
var existingDocumentUri = testWorkspace.CurrentSolution.GetRequiredDocument(testWorkspace.Documents.Single().Id).GetURI();
var workspaceEdit = new WorkspaceEdit()
{
DocumentChanges = new SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>[]
{
// Create file
new CreateFile() { Uri = newDocumentUri },
// Add content to file
new TextDocumentEdit()
{
TextDocument = new OptionalVersionedTextDocumentIdentifier { Uri = newDocumentUri },
Edits = new TextEdit[]
{
new TextEdit()
{
Range = new LSP.Range
{
Start = new Position()
{
Line = 0,
Character = 0,
},
End = new Position()
{
Line = 0,
Character = 0
}
},
NewText = @"class ABC
{
}
"
}
}
},
// Remove the declaration from existing file
new TextDocumentEdit()
{
TextDocument = new OptionalVersionedTextDocumentIdentifier() { Uri = existingDocumentUri },
Edits = new TextEdit[]
{
new TextEdit()
{
Range = new LSP.Range
{
Start = new Position()
{
Line = 0,
Character = 0,
},
End = new Position()
{
Line = 4,
Character = 0
}
},
NewText = ""
}
}
}
}
};

var expectedCodeAction = CodeActionsTests.CreateCodeAction(
title: string.Format(FeaturesResources.Move_type_to_0, "ABC.cs"),
kind: CodeActionKind.Refactor,
children: Array.Empty<LSP.VSInternalCodeAction>(),
data: CreateCodeActionResolveData(
string.Format(FeaturesResources.Move_type_to_0, "ABC.cs"),
testLspServer.GetLocations("caret").Single()),
priority: VSInternalPriorityLevel.Normal,
groupName: "Roslyn2",
applicableRange: new LSP.Range { Start = new Position { Line = 0, Character = 6 }, End = new Position { Line = 0, Character = 9 } },
diagnostics: null,
edit: workspaceEdit);

AssertJsonEquals(expectedCodeAction, actualResolvedAction);
}

private static async Task<LSP.VSInternalCodeAction> RunGetCodeActionResolveAsync(
TestLspServer testLspServer,
VSInternalCodeAction unresolvedCodeAction)
Expand Down