Skip to content

Commit

Permalink
Merge branch 'main' into server-error-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
belav authored Apr 7, 2024
2 parents 3603474 + e95599a commit cefc1cf
Show file tree
Hide file tree
Showing 33 changed files with 8,163 additions and 18,135 deletions.
12 changes: 6 additions & 6 deletions Src/CSharpier.Cli.Tests/CliTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ public async Task Should_Format_Unicode()
result.ExitCode.Should().Be(0);
}

[Test]
public async Task Should_Print_NotFound()
[TestCase("BasicFile.cs")]
[TestCase("./BasicFile.cs")]
[TestCase("/BasicFile.cs")]
public async Task Should_Print_NotFound(string path)
{
var result = await new CsharpierProcess().WithArguments("/BasicFile.cs").ExecuteAsync();
var result = await new CsharpierProcess().WithArguments(path).ExecuteAsync();

result.Output.Should().BeEmpty();
result
.ErrorOutput.Should()
.StartWith("There was no file or directory found at /BasicFile.cs");
result.ErrorOutput.Should().StartWith("There was no file or directory found at " + path);
result.ExitCode.Should().Be(1);
}

Expand Down
3 changes: 2 additions & 1 deletion Src/CSharpier.Cli/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ CancellationToken cancellationToken
if (!isFile && !isDirectory)
{
console.WriteErrorLine(
"There was no file or directory found at " + directoryOrFilePath
"There was no file or directory found at "
+ commandLineOptions.OriginalDirectoryOrFilePaths[x]
);
return 1;
}
Expand Down
9 changes: 1 addition & 8 deletions Src/CSharpier.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,7 @@ CancellationToken cancellationToken
}
else
{
directoryOrFile = directoryOrFile!
.Select(o =>
o == "."
// .csharpierignore gets confused by . so just don't include it
? Directory.GetCurrentDirectory()
: Path.GetFullPath(o)
)
.ToArray();
directoryOrFile = directoryOrFile!.Select(Path.GetFullPath).ToArray();
}

var commandLineOptions = new CommandLineOptions
Expand Down
2,294 changes: 442 additions & 1,852 deletions Src/CSharpier.Playground/ClientApp/package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Src/CSharpier.Playground/ClientApp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"react-json-view": "1.21.3",
"react-router-dom": "5.2.0",
"rimraf": "3.0.2",
"sass": "^1.52.1",
"sass": "1.52.1",
"typescript": "4.1.3",
"vite": "2.9.9",
"vite": "2.9.17",
"vite-plugin-svgr": "2.1.0"
},
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions Src/CSharpier.Rider/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

# csharpier-rider Changelog

## [1.6.4]
- Better support for format on save - now works when files are saved via building/running the project.

## [1.6.3]
- Fix issue with occasional exception "java.lang.IllegalStateException: |E| Wrong thread RdOptionalProperty"

## [1.6.2]
- Fix issues with lookup of '.NET CLI executable path', csharpier will now wait until rider is ready with the information.
- No more falling back to `PATH`
Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier.Rider/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

pluginGroup = com.intellij.csharpier
pluginName = csharpier
pluginVersion = 1.6.2
pluginVersion = 1.6.4

# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.StartupActivity
import com.intellij.openapi.startup.StartupActivity.DumbAware
import com.intellij.util.application
import com.jetbrains.rd.platform.util.lifetime
import com.jetbrains.rd.util.reactive.adviseUntil
import com.jetbrains.rider.model.riderSolutionLifecycle
Expand All @@ -16,25 +17,26 @@ class CSharpierStartup : StartupActivity, DumbAware {
var logger: Logger = CSharpierLogger.getInstance()

override fun runActivity(project: Project) {
project.solution.riderSolutionLifecycle.isProjectModelReady.adviseUntil(project.lifetime) { isReady ->
// this ensures we meet the conditions of isProjectModelReady "Must be executed on UI thread or background threads with special permissions"
application.invokeLater {
project.solution.riderSolutionLifecycle.isProjectModelReady.adviseUntil(project.lifetime) { isReady ->

val dotNetCoreRuntime =
RiderDotNetActiveRuntimeHost.Companion.getInstance(project).dotNetCoreRuntime.value
val dotNetCoreRuntime =
RiderDotNetActiveRuntimeHost.Companion.getInstance(project).dotNetCoreRuntime.value

if (!isReady || dotNetCoreRuntime == null) {
if (isReady) {
logger.warn("isProjectModelReady is true, but dotNetCoreRuntime is still null");
}
if (!isReady || dotNetCoreRuntime == null) {
if (isReady) {
logger.warn("isProjectModelReady is true, but dotNetCoreRuntime is still null");
}

return@adviseUntil false
}
return@adviseUntil false
}

CSharpierProcessProvider.getInstance(project)
ApplicationManager.getApplication().getService(ReformatWithCSharpierOnSave::class.java)
DotNetProvider.getInstance(project).initialize();
CSharpierProcessProvider.getInstance(project)
DotNetProvider.getInstance(project).initialize();

return@adviseUntil true
return@adviseUntil true
}
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.intellij.csharpier;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileDocumentManagerListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectUtil;
import org.jetbrains.annotations.NotNull;

public class ReformatWithCSharpierOnSaveListener implements FileDocumentManagerListener {
Logger logger = CSharpierLogger.getInstance();

@Override
public void beforeDocumentSaving(@NotNull Document document) {
var project = this.GetProject(document);
if (project == null) {
return;
}

var cSharpierSettings = CSharpierSettings.getInstance(project);
if (!cSharpierSettings.getRunOnSave()) {
return;
}

var formattingService = FormattingService.getInstance(project);
this.logger.debug("beforeDocumentSaving for " + document);
formattingService.format(document, project);
}

private Project GetProject(@NotNull Document document) {
var virtualFile = FileDocumentManager.getInstance().getFile(document);
if (virtualFile == null) {
return null;
}

return ProjectUtil.guessProjectForContentFile(virtualFile);
}

}
4 changes: 2 additions & 2 deletions Src/CSharpier.Rider/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</action>
</actions>
<applicationListeners>
<listener class="com.intellij.csharpier.ReformatWithCSharpierOnSave"
topic="com.intellij.openapi.actionSystem.ex.AnActionListener" />
<listener class="com.intellij.csharpier.ReformatWithCSharpierOnSaveListener"
topic="com.intellij.openapi.fileEditor.FileDocumentManagerListener"/>
</applicationListeners>
</idea-plugin>
28 changes: 28 additions & 0 deletions Src/CSharpier.Tests/CodeFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,34 @@ public void Format_Should_Use_EndOfLine()
result.Code.Should().Be("var someVariable = someValue;\r\n");
}

[Test]
public void Format_Should_Ignore_Generated_Files()
{
var code = """
// <auto-generated>
var someVariable = someValue;
""";
var result = CodeFormatter.Format(code, new CodeFormatterOptions());

result.Code.Should().Be(code);
}

[Test]
public void Format_Should_Format_Generated_Files()
{
var code = """
// <auto-generated>
var someVariable = someValue;
""";
var result = CodeFormatter.Format(
code,
new CodeFormatterOptions { IncludeGenerated = true }
);

result.Code.Should().Be(code.Replace(" = ", " = "));
}

[TestCase("\n")]
[TestCase("\r\n")]
public void Format_Should_Get_Line_Endings_With_SyntaxTree(string lineEnding)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ public class ClassName
#endregion one
;
}

class ClassName
{
#region Region
// csharpier-ignore-start
public string Field;
// csharpier-ignore-end
#endregion
}
3 changes: 2 additions & 1 deletion Src/CSharpier/CodeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public static Task<CodeFormatterResult> FormatAsync(
Width = options.Width,
UseTabs = options.IndentStyle == IndentStyle.Tabs,
TabWidth = options.IndentSize,
EndOfLine = options.EndOfLine
EndOfLine = options.EndOfLine,
IncludeGenerated = options.IncludeGenerated
},
cancellationToken
);
Expand Down
1 change: 1 addition & 0 deletions Src/CSharpier/CodeFormatterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class CodeFormatterOptions
public IndentStyle IndentStyle { get; init; } = IndentStyle.Spaces;
public int IndentSize { get; init; } = 4;
public EndOfLine EndOfLine { get; init; } = EndOfLine.Auto;
public bool IncludeGenerated { get; init; }
}

public enum IndentStyle
Expand Down
15 changes: 12 additions & 3 deletions Src/CSharpier/DocPrinter/DocPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class DocPrinter
protected readonly string EndOfLine;
protected readonly PrinterOptions PrinterOptions;
protected readonly Indenter Indenter;
protected Stack<Indent> RegionIndents = new();
protected readonly Stack<Indent> RegionIndents = new();

protected DocPrinter(Doc doc, PrinterOptions printerOptions, string endOfLine)
{
Expand Down Expand Up @@ -156,8 +156,17 @@ private void ProcessNextCommand()
{
if (region.IsEnd)
{
var regionIndent = this.RegionIndents.Pop();
this.Output.Append(regionIndent.Value);
// in the case where regions are combined with ignored ranges, the start region
// ends up printing inside the unformatted nodes, so we don't have a matching
// start region to go with this end region
if (this.RegionIndents.TryPop(out var regionIndent))
{
this.Output.Append(regionIndent.Value);
}
else
{
this.Output.Append(indent.Value);
}
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion Src/CSharpier/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

CSharpier.CodeFormatterOptions.IncludeGenerated.get -> bool
CSharpier.CodeFormatterOptions.IncludeGenerated.init -> void
22 changes: 22 additions & 0 deletions Src/CSharpier/Utilities/StackExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace CSharpier.Utilities;

using System.Diagnostics.CodeAnalysis;

internal static class StackExtensions
{
#if NETSTANDARD2_0
public static bool TryPop<T>(this Stack<T> stack, [NotNullWhen(true)] out T? result)
{
if (stack.Count > 0)
{
result = stack.Pop()!;
return true;
}
else
{
result = default;
return false;
}
}
#endif
}
4 changes: 2 additions & 2 deletions Src/Website/docs/About.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ CSharpier can also format [on save in your editor](https://csharpier.com/docs/Ed
---

### Before
```c#
```csharp
public class ClassName {
public void CallMethod() {
var shuffle = shuffle.Skip(26).LogQuery("Bottom Half").InterleaveSequenceWith(shuffle.Take(26).LogQuery("Top Half"), shuffle.Skip(26).LogQuery("Bottom Half")).LogQuery("Shuffle").ToArray();
Expand All @@ -39,7 +39,7 @@ public class ClassName {
```

### After
```c#
```csharp
public class ClassName
{
public void CallMethod()
Expand Down
8 changes: 4 additions & 4 deletions Src/Website/docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ hide_table_of_contents: true
Use the `dotnet csharpier` command to run CSharpier from the command line.

In practice, it will look something like:
```
```shell
dotnet csharpier .
```
This command will format all c# files in the current directory and its children.
Expand Down Expand Up @@ -37,8 +37,8 @@ Options:

```

### \[<directoryOrFile\>]
### `[<directoryOrFile\>]`

If a list of paths is supplied
- if the path points to an existing file, CSharpier will format that file
- if the path points to an existing directory, CSharpier will recursively format the contents of that directory
Expand Down Expand Up @@ -109,7 +109,7 @@ By default CSharpier will format files in place. This option allows you to write
If you pipe input to CSharpier it will also write the formatting results to stdout.

*TestFile.cs*
```c#
```csharp
public class ClassName
{
public string Field;
Expand Down
Loading

0 comments on commit cefc1cf

Please sign in to comment.