Skip to content

Commit

Permalink
Merge branch 'main' into msbuild-compile-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
belav authored Sep 17, 2023
2 parents 5658068 + 2ad4e5d commit c716b63
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 63 deletions.
44 changes: 39 additions & 5 deletions Docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ hide_table_of_contents: true
CSharpier has support for a configuration file. You can use any of the following files
- A ```.csharpierrc``` file in JSON or YAML.
- A ```.csharpierrc.json``` or ```.csharpierrc.yaml``` file.
- A ```.editorconfig``` file. See EditorConfig section below.

The configuration file will be resolved starting from the location of the file being formatted, and searching up the file tree until a config file is (or isn’t) found.

Expand All @@ -14,35 +15,49 @@ JSON
{
"printWidth": 100,
"useTabs": false,
"tabWidth": 4
"tabWidth": 4,
"endOfLine": "auto"
}
```
YAML
```yaml
printWidth: 100
useTabs: false
tabWidth: 4
endOfLine: auto
```
#### Print Width
Specify at what point the printer will wrap content. This is not a hard limit. Some lines will be shorter or longer.
Default 100
Default `100`
#### Use Tabs
_First available in 0.17.0_

Indent lines with tabs instead of spaces.

Default false
Default `false`
#### Tab Width
_First available in 0.17.0_

Specify the number of spaces used per indentation level.

Default 4
Default `4`

#### End of Line
_First available in 0.26.0_

Valid options:

- "auto" - Maintain existing line endings (mixed values within one file are normalised by looking at what's used after the first line)
- "lf" – Line Feed only (\n), common on Linux and macOS as well as inside git repos
- "crlf" - Carriage Return + Line Feed characters (\r\n), common on Windows

Default `auto`


#### Preprocessor Symbol Sets
**Removed in 0.25.0**
_Removed in 0.25.0_

Currently CSharpier only has basic support for understanding how to format code inside of `#if` directives.
It will attempt to determine which sets of preprocessor symbols are needed for roslyn to parse all the code in each file.
Expand All @@ -62,3 +77,22 @@ For example in the following code block, the following symbol sets would be need
When supplying symbol sets, they will be used for all files being formatted. This will slow down formatting, and determining all symbol sets needed across all files won't be straight forward.

The long term plan is to improve Csharpier's ability to determine the symbol sets itself and to allow specifying them for individual files.

### EditorConfig

CSharpier supports configuration via an `.editorconfig` file. A `.csahrpierrc*` file in the same directory will take priority.

```ini
[*]
# Non-configurable behaviors
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
# Configurable behaviors
# end_of_line = lf - there is no 'auto' with a .editorconfig
indent_style = space
indent_size = 4
max_line_length = 100
```
12 changes: 12 additions & 0 deletions Src/CSharpier.Cli/EditorConfig/EditorConfigSections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public PrinterOptions ConvertToPrinterOptions(string filePath)
printerOptions.TabWidth = resolvedConfiguration.IndentSize ?? printerOptions.TabWidth;
}

if (resolvedConfiguration.EndOfLine is { } endOfLine)
{
printerOptions.EndOfLine = endOfLine;
}

return printerOptions;
}

Expand All @@ -43,6 +48,7 @@ private class ResolvedConfiguration
public int? IndentSize { get; }
public int? TabWidth { get; }
public int? MaxLineLength { get; }
public EndOfLine? EndOfLine { get; }

public ResolvedConfiguration(List<Section> sections)
{
Expand Down Expand Up @@ -81,6 +87,12 @@ public ResolvedConfiguration(List<Section> sections)
? tabWidthValue
: this.IndentSize;
}

var endOfLine = sections.LastOrDefault(o => o.EndOfLine != null)?.EndOfLine;
if (Enum.TryParse(endOfLine, true, out EndOfLine result))
{
this.EndOfLine = result;
}
}
}
}
2 changes: 2 additions & 0 deletions Src/CSharpier.Cli/EditorConfig/Section.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class Section
public string? IndentSize { get; }
public string? TabWidth { get; }
public string? MaxLineLength { get; }
public string? EndOfLine { get; }

public Section(SectionData section, string directory)
{
Expand All @@ -20,6 +21,7 @@ public Section(SectionData section, string directory)
this.IndentSize = section.Keys["indent_size"];
this.TabWidth = section.Keys["tab_width"];
this.MaxLineLength = section.Keys["max_line_length"];
this.EndOfLine = section.Keys["end_of_line"];
}

public bool IsMatch(string fileName)
Expand Down
36 changes: 36 additions & 0 deletions Src/CSharpier.Cli/Options/CaseInsensitiveEnumConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace CSharpier.Cli.Options;

using System.Text.Json;
using System.Text.Json.Serialization;

internal class CaseInsensitiveEnumConverter<TEnum> : JsonConverter<TEnum>
where TEnum : struct
{
public override TEnum Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
if (reader.TokenType != JsonTokenType.String)
{
throw new JsonException(
$"Expected a string for enum parsing, but got {reader.TokenType}."
);
}

var enumText = reader.GetString();
if (Enum.TryParse(enumText, ignoreCase: true, out TEnum result))
{
return result;
}

throw new JsonException($"Unable to parse '{enumText}' to enum of type {typeof(TEnum)}.");
}

public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options)
{
var enumText = value.ToString();
writer.WriteStringValue(enumText);
}
}
8 changes: 6 additions & 2 deletions Src/CSharpier.Cli/Options/ConfigurationFileOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ namespace CSharpier.Cli.Options;

using System.IO.Abstractions;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Logging;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

public class ConfigurationFileOptions
internal class ConfigurationFileOptions
{
public int PrintWidth { get; init; } = 100;
public int TabWidth { get; init; } = 4;
public bool UseTabs { get; init; }

[JsonConverter(typeof(CaseInsensitiveEnumConverter<EndOfLine>))]
public EndOfLine EndOfLine { get; init; }

private static readonly string[] validExtensions = { ".csharpierrc", ".json", ".yml", ".yaml" };

internal static PrinterOptions CreatePrinterOptionsFromPath(
Expand All @@ -34,7 +38,7 @@ ConfigurationFileOptions configurationFileOptions
TabWidth = configurationFileOptions.TabWidth,
UseTabs = configurationFileOptions.UseTabs,
Width = configurationFileOptions.PrintWidth,
EndOfLine = EndOfLine.Auto
EndOfLine = configurationFileOptions.EndOfLine
};
}

Expand Down
3 changes: 3 additions & 0 deletions Src/CSharpier.Rider/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

# csharpier-rider Changelog

## [1.3.10]
- Read from error stream to prevent the plugin hanging when the csharpier process writes too much to the error stream.

## [1.3.9]
- Wait at most 3 seconds for csharpier to format otherwise consider it hung and restart it.

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 @@ -4,7 +4,7 @@
pluginGroup = com.intellij.csharpier
pluginName = csharpier
# SemVer format -> https://semver.org
pluginVersion = 1.3.9
pluginVersion = 1.3.10

# 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 @@ -3,16 +3,8 @@
import com.intellij.openapi.Disposable;
import com.intellij.openapi.diagnostic.Logger;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class CSharpierProcessPipeMultipleFiles implements ICSharpierProcess, Disposable {
private final boolean useUtf8;
Expand All @@ -39,6 +31,12 @@ private void startProcess() {

this.stdin = new OutputStreamWriter(this.process.getOutputStream(), charset);
this.stdOut = new BufferedReader(new InputStreamReader(this.process.getInputStream(), charset));

// if we don't read the error stream, eventually too much is buffered on it and the plugin hangs
var errorGobbler = new StreamGobbler(this.process.getErrorStream());
errorGobbler.start();


} catch (Exception e) {
this.logger.error("error", e);
}
Expand Down Expand Up @@ -109,4 +107,22 @@ public void dispose() {
this.process.destroy();
}
}

private class StreamGobbler extends Thread {
InputStream inputStream;

private StreamGobbler(InputStream inputStream) {
this.inputStream = inputStream;
}

@Override
public void run() {
try {
var streamReader = new InputStreamReader(this.inputStream);
var reader = new BufferedReader(streamReader);
while (reader.readLine() != null) {}
}
catch (IOException ioe) { }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.intellij.csharpier;

import com.intellij.application.Topics;
import com.intellij.ide.actions.SaveAllAction;
import com.intellij.ide.actions.SaveDocumentAction;
import com.intellij.openapi.actionSystem.AnAction;
Expand All @@ -15,11 +14,6 @@ public class ReformatWithCSharpierOnSave implements AnActionListener {

private final Logger logger = CSharpierLogger.getInstance();

public ReformatWithCSharpierOnSave() {
// TODO this is deprecated and should be switched to https://plugins.jetbrains.com/docs/intellij/messaging-infrastructure.html#subscribing-to-a-topic
Topics.subscribe(AnActionListener.TOPIC, null, this);
}

@Override
public void beforeActionPerformed(@NotNull AnAction action, @NotNull AnActionEvent event) {
if (action instanceof SaveDocumentAction) {
Expand Down
5 changes: 4 additions & 1 deletion Src/CSharpier.Rider/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<depends>com.intellij.modules.rider</depends>

<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="com.intellij.csharpier.ReformatWithCSharpierOnSave"/>
<projectService serviceImplementation="com.intellij.csharpier.CSharpierProcessProvider"/>
<projectService serviceImplementation="com.intellij.csharpier.FormattingService"/>
<projectService serviceImplementation="com.intellij.csharpier.CSharpierSettings"/>
Expand All @@ -26,4 +25,8 @@
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
</action>
</actions>
<applicationListeners>
<listener class="com.intellij.csharpier.ReformatWithCSharpierOnSave"
topic="com.intellij.openapi.actionSystem.ex.AnActionListener" />
</applicationListeners>
</idea-plugin>
11 changes: 7 additions & 4 deletions Src/CSharpier.Tests/OptionsProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ public async Task Should_Return_Json_Extension_Options()
"c:/test/.csharpierrc.json",
@"{
""printWidth"": 10,
""preprocessorSymbolSets"": [""1,2"", ""3""]
""endOfLine"": ""crlf""
}"
);

var result = await context.CreateProviderAndGetOptionsFor("c:/test", "c:/test/test.cs");

result.Width.Should().Be(10);
result.EndOfLine.Should().Be(EndOfLine.CRLF);
}

[TestCase("yaml")]
Expand All @@ -68,15 +69,14 @@ public async Task Should_Return_Yaml_Extension_Options(string extension)
$"c:/test/.csharpierrc.{extension}",
@"
printWidth: 10
preprocessorSymbolSets:
- 1,2
- 3
endOfLine: crlf
"
);

var result = await context.CreateProviderAndGetOptionsFor("c:/test", "c:/test/test.cs");

result.Width.Should().Be(10);
result.EndOfLine.Should().Be(EndOfLine.CRLF);
}

[TestCase("{ \"printWidth\": 10 }")]
Expand Down Expand Up @@ -203,6 +203,7 @@ public async Task Should_Support_EditorConfig_Basic()
indent_style = space
indent_size = 2
max_line_length = 10
end_of_line = crlf
"
);

Expand All @@ -211,6 +212,7 @@ public async Task Should_Support_EditorConfig_Basic()
result.UseTabs.Should().BeFalse();
result.TabWidth.Should().Be(2);
result.Width.Should().Be(10);
result.EndOfLine.Should().Be(EndOfLine.CRLF);
}

[TestCase("tab_width")]
Expand Down Expand Up @@ -459,6 +461,7 @@ private static void ShouldHaveDefaultOptions(PrinterOptions printerOptions)
printerOptions.Width.Should().Be(100);
printerOptions.TabWidth.Should().Be(4);
printerOptions.UseTabs.Should().BeFalse();
printerOptions.EndOfLine.Should().Be(EndOfLine.Auto);
}

private class TestContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="83d6b6a0-9e25-4034-80f3-38445d8a8837" Version="1.4.5" Language="en-US" Publisher="CSharpier" />
<Identity Id="83d6b6a0-9e25-4034-80f3-38445d8a8837" Version="1.4.7" Language="en-US" Publisher="CSharpier" />
<DisplayName>CSharpier</DisplayName>
<Description xml:space="preserve">CSharpier is an opinionated code formatter for c#. It uses Roslyn to parse your code and re-prints it using its own rules.</Description>
<MoreInfo>https://github.com/belav/csharpier</MoreInfo>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MinimumVisualStudioVersion>16.0</MinimumVisualStudioVersion>
Expand Down Expand Up @@ -42,7 +42,7 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE;DEV16</DefineConstants>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
Expand Down
Loading

0 comments on commit c716b63

Please sign in to comment.