Skip to content

Commit bcba38c

Browse files
committed
Create a command to return the project directory (#78)
1 parent 49aa9ee commit bcba38c

File tree

7 files changed

+27
-31
lines changed

7 files changed

+27
-31
lines changed

src/DotRush.Common/MSBuild/MSBuildProject.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
using System.Text.Json.Serialization;
2-
using SystemPath = System.IO.Path;
32

43
namespace DotRush.Common.MSBuild;
54

65
public class MSBuildProject {
7-
[JsonPropertyName("name")] public string Name { get; set; }
8-
[JsonPropertyName("path")] public string Path { get; set; }
6+
[JsonPropertyName("name")] public string Name { get; init; }
7+
[JsonPropertyName("path")] public string FilePath { get; init; }
8+
[JsonPropertyName("directory")] public string? DirectoryPath { get; init; }
99
[JsonPropertyName("frameworks")] public IEnumerable<string> Frameworks { get; set; }
1010
[JsonPropertyName("configurations")] public IEnumerable<string> Configurations { get; set; }
1111
[JsonPropertyName("isLegacyFormat")] public bool IsLegacyFormat { get; set; }
1212

13-
[JsonIgnore] public string Directory => SystemPath.GetDirectoryName(Path)!;
14-
1513
internal MSBuildProject(string path) {
14+
Name = Path.GetFileNameWithoutExtension(path);
15+
FilePath = Path.GetFullPath(path);
16+
DirectoryPath = Path.GetDirectoryName(FilePath);
1617
Frameworks = Enumerable.Empty<string>();
1718
Configurations = Enumerable.Empty<string>();
18-
Name = SystemPath.GetFileNameWithoutExtension(path);
19-
Path = SystemPath.GetFullPath(path);
2019
}
2120

2221
public bool IsExecutable() {
@@ -33,6 +32,6 @@ public bool IsTestProject() {
3332
return this.HasPackage("NUnit") || this.HasPackage("NUnitLite") || this.HasPackage("xunit");
3433
}
3534
public string GetAssemblyName() {
36-
return this.EvaluateProperty("AssemblyName", SystemPath.GetFileNameWithoutExtension(Path)) ?? string.Empty;
35+
return this.EvaluateProperty("AssemblyName", Path.GetFileNameWithoutExtension(FilePath)) ?? string.Empty;
3736
}
3837
}

src/DotRush.Common/MSBuild/MSBuildProjectsLoader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public static class MSBuildProjectsLoader {
1818
}
1919

2020
private static bool IsLegacyFormat(MSBuildProject project) {
21-
var document = XDocument.Load(project.Path);
21+
var document = XDocument.Load(project.FilePath);
2222
if (document.Root == null || !document.Root.HasAttributes)
2323
return false;
24-
24+
2525
return !document.Root.Attributes().Any(a => a.Name.LocalName == "Sdk");
2626
}
2727

src/DotRush.Common/MSBuild/MSBuildPropertyEvaluator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace DotRush.Common.MSBuild;
66

77
public static class MSBuildPropertyEvaluator {
88
public static string? EvaluateProperty(this MSBuildProject project, string name, string? defaultValue = null) {
9-
var propertyMatches = project.GetPropertyMatches(project.Path, name);
9+
var propertyMatches = project.GetPropertyMatches(project.FilePath, name);
1010
if (propertyMatches == null)
1111
return defaultValue;
1212

@@ -17,7 +17,7 @@ public static class MSBuildPropertyEvaluator {
1717
return propertyValue;
1818
}
1919
public static bool HasPackage(this MSBuildProject project, string packageName) {
20-
return project.HasPackageMatches(project.Path, packageName);
20+
return project.HasPackageMatches(project.FilePath, packageName);
2121
}
2222

2323
private static MatchCollection? GetPropertyMatches(this MSBuildProject project, string projectPath, string propertyName, bool isEndPoint = false) {
@@ -91,7 +91,7 @@ private static bool HasPackageMatches(this MSBuildProject project, string projec
9191

9292
return project.HasPackageMatches(propsFile, packageName, true);
9393
}
94-
94+
9595
private static string GetPropertyValue(this MSBuildProject project, string propertyName, MatchCollection matches) {
9696
var includeRegex = new Regex(@"\$\((?<inc>.*?)\)");
9797
var resultSequence = new StringBuilder();

src/DotRush.Roslyn.Server/Handlers/TextDocument/HoverHandler.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using EmmyLua.LanguageServer.Framework.Server.Handler;
1212
using Microsoft.CodeAnalysis;
1313
using Microsoft.CodeAnalysis.FindSymbols;
14-
using SymbolKind = Microsoft.CodeAnalysis.SymbolKind;
1514

1615
namespace DotRush.Roslyn.Server.Handlers.TextDocument;
1716

@@ -31,55 +30,50 @@ public override void RegisterCapability(ServerCapabilities serverCapabilities, C
3130
if (documentIds == null)
3231
return null;
3332

34-
var displayStrings = new Dictionary<string, List<string>>();
33+
var displayDictionary = new Dictionary<string, List<string>>();
3534
var documentation = string.Empty;
3635
foreach (var documentId in documentIds) {
3736
var document = navigationService.Solution?.GetDocument(documentId);
3837
if (document == null)
3938
continue;
4039

4140
var sourceText = await document.GetTextAsync(token).ConfigureAwait(false);
42-
var semanticModel = await document.GetSemanticModelAsync(token).ConfigureAwait(false);
4341
var offset = request.Position.ToOffset(sourceText);
4442
var symbol = await SymbolFinder.FindSymbolAtPositionAsync(document, offset).ConfigureAwait(false);
45-
if (symbol == null || semanticModel == null)
43+
if (symbol == null)
4644
continue;
4745

4846
if (symbol is IAliasSymbol aliasSymbol)
4947
symbol = aliasSymbol.Target;
5048

51-
var displayString = symbol.Kind == SymbolKind.NamedType || symbol.Kind == SymbolKind.Namespace
52-
? symbol.ToDisplayString(DisplayFormat.Default)
53-
: symbol.ToMinimalDisplayString(semanticModel, offset, DisplayFormat.Minimal);
49+
var format = symbol.Kind == SymbolKind.NamedType || symbol.Kind == SymbolKind.Namespace ? DisplayFormat.Default : DisplayFormat.Minimal;
50+
var displayString = symbol.ToDisplayString(format);
51+
if (!displayDictionary.ContainsKey(displayString))
52+
displayDictionary[displayString] = new List<string>();
5453

55-
if (!displayStrings.ContainsKey(displayString))
56-
displayStrings.Add(displayString, new List<string>());
54+
displayDictionary[displayString].Add(document.Project.GetTargetFramework());
5755

58-
displayStrings[displayString].Add(document.Project.GetTargetFramework());
5956
if (string.IsNullOrEmpty(documentation))
6057
documentation = symbol.GetDocumentationCommentXml();
6158
}
6259

63-
if (displayStrings.Count == 1) {
60+
if (displayDictionary.Count == 1) {
6461
return new HoverResponse {
6562
Contents = new MarkupContent {
6663
Kind = MarkupKind.Markdown,
67-
Value = MarkdownExtensions.CreateDocumentation(displayStrings.Keys.First(), documentation, "csharp")
64+
Value = MarkdownExtensions.CreateDocumentation(displayDictionary.Keys.First(), documentation, "csharp")
6865
}
6966
};
7067
}
7168

72-
if (displayStrings.Count > 1) {
69+
if (displayDictionary.Count > 1) {
7370
var builder = new StringBuilder();
74-
foreach (var pair in displayStrings) {
75-
var frameworks = string.Join(", ", pair.Value);
76-
builder.AppendLine(MarkdownExtensions.CreateDocumentation($"{pair.Key} ({frameworks})", null, "csharp"));
77-
}
71+
displayDictionary.ForEach(kv => builder.AppendLine($"{kv.Key} ({string.Join(", ", kv.Value)})"));
7872

7973
return new HoverResponse {
8074
Contents = new MarkupContent {
8175
Kind = MarkupKind.Markdown,
82-
Value = builder.ToString()
76+
Value = MarkdownExtensions.CreateDocumentation(builder.ToString(), null, "csharp")
8377
}
8478
};
8579
}

src/VSCode/controllers/statusbarController.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class StatusBarController {
2525
context.subscriptions.push(vscode.window.registerFileDecorationProvider(StatusBarController.projectDecorationProvider));
2626
context.subscriptions.push(vscode.commands.registerCommand(res.commandIdSelectActiveConfiguration, StatusBarController.showQuickPickConfiguration));
2727
context.subscriptions.push(vscode.commands.registerCommand(res.commandIdActiveProjectPath, () => StatusBarController.activeProject?.path));
28+
context.subscriptions.push(vscode.commands.registerCommand(res.commandIdActiveProjectDirectory, () => StatusBarController.activeProject?.directory));
2829
context.subscriptions.push(vscode.commands.registerCommand(res.commandIdActiveConfiguration, () => StatusBarController.activeConfiguration));
2930
context.subscriptions.push(vscode.commands.registerCommand(res.commandIdActiveTargetFramework, () => StatusBarController.activeFramework));
3031
context.subscriptions.push(vscode.workspace.onDidSaveTextDocument(async e => {

src/VSCode/models/project.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as path from "path";
55
export interface Project {
66
name: string;
77
path: string;
8+
directory: string;
89
frameworks: string[];
910
configurations: string[];
1011
}

src/VSCode/resources/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const commandTitleSelectActiveFramework = "Select target framework";
2727
export const commandIdSelectActiveConfiguration = "dotrush.selectActiveConfiguration";
2828
export const commandTitleSelectActiveConfiguration = "select configuration";
2929
export const commandIdActiveProjectPath = "dotrush.activeProjectPath";
30+
export const commandIdActiveProjectDirectory = "dotrush.activeProjectDirectory";
3031
export const commandIdActiveConfiguration = "dotrush.activeConfiguration";
3132
export const commandIdActiveTargetFramework = "dotrush.activeTargetFramework";
3233
export const commandIdActiveTargetPath = "dotrush.activeTargetPath";

0 commit comments

Comments
 (0)