Skip to content

Commit 0de7248

Browse files
committed
Fix an issue where a new file was added to wrong project in solution
The issue happened when there were two projects in solution: - Project/Project.csproj - ProjectTwo/ProjectTwo.csproj When adding ProjectTwo/Class.cs it would add file to Project/Project.csproj instead of ProjectTwo as it should.
1 parent d9ff961 commit 0de7248

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ with the next provider to be more resilent;
1313
- move static members to another class;
1414
- generate overrides;
1515
- others;
16+
* Fix an issue where a new file was added to wrong project.
1617

1718
## [0.5.5] - 2022-08-23 / Prienai
1819
* Fix intermittent server crashes after upgrading to latest Ionide.LanguageServerProtocol:

src/CSharpLanguageServer/RoslynHelpers.fs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ let getFullReflectionName (containingType: INamedTypeSymbol) =
908908

909909
String.Join(".", stack)
910910

911-
let tryAddDocument _logMessage
911+
let tryAddDocument logMessage
912912
(docFilePath: string)
913913
(text: string)
914914
(solution: Solution)
@@ -917,23 +917,30 @@ let tryAddDocument _logMessage
917917
let docDir = Path.GetDirectoryName(docFilePath)
918918
//logMessage (sprintf "TextDocumentDidOpen: docFilename=%s docDir=%s" docFilename docDir)
919919

920-
let matchesPath (p: Project) =
920+
let fileOnProjectDir (p: Project) =
921921
let projectDir = Path.GetDirectoryName(p.FilePath)
922-
(docDir |> string).StartsWith(projectDir |> string)
922+
let projectDirWithDirSepChar = projectDir + (string Path.DirectorySeparatorChar)
923923

924-
let projectOnPath = solution.Projects |> Seq.filter matchesPath |> Seq.tryHead
924+
(docDir = projectDir) || docDir.StartsWith(projectDirWithDirSepChar)
925+
926+
let projectOnPath =
927+
solution.Projects
928+
|> Seq.filter fileOnProjectDir
929+
|> Seq.tryHead
925930

926931
match projectOnPath with
927932
| Some proj ->
928933
let projectBaseDir = Path.GetDirectoryName(proj.FilePath)
929934
let docName = docFilePath.Substring(projectBaseDir.Length+1)
930935

931-
//logMessage (sprintf "Adding file %s (\"%s\") to project %s" docName docFilePath proj.FilePath)
936+
logMessage (sprintf "Adding \"%s\" (\"%s\") to project %s" docName docFilePath proj.FilePath)
932937

933938
let newDoc = proj.AddDocument(name=docName, text=SourceText.From(text), folders=null, filePath=docFilePath)
934939
Some newDoc
935940

936-
| None -> None
941+
| None ->
942+
logMessage (sprintf "No parent project could be resolved to add file \"%s\" to workspace" docFilePath)
943+
None
937944

938945
let processChange (oldText: SourceText) (change: TextChange) : TextEdit =
939946
let mapToTextEdit(linePosition: LinePositionSpan, newText: string) : TextEdit =

src/CSharpLanguageServer/Server.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,9 @@ let setupServerHandlers options (lspClient: LspClient) =
306306
// ok, this document is not on solution, register a new one
307307
let docFilePath = openParams.TextDocument.Uri.Substring("file://".Length)
308308
let newDocMaybe = tryAddDocument logMessage
309-
docFilePath
310-
openParams.TextDocument.Text
311-
scope.Solution
309+
docFilePath
310+
openParams.TextDocument.Text
311+
scope.Solution
312312
match newDocMaybe with
313313
| Some newDoc ->
314314
scope.Emit(SolutionChange newDoc.Project.Solution)

0 commit comments

Comments
 (0)