Skip to content

Commit

Permalink
Working on rider issues
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Nov 20, 2023
1 parent 14d9a79 commit cc2e389
Show file tree
Hide file tree
Showing 18 changed files with 17,127 additions and 2,906 deletions.
7 changes: 7 additions & 0 deletions CSharpier.sln
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shell", "Shell\Shell.csproj
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Src", "Src", "{0D12A95B-A237-4181-937C-DF6D0265975E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Website", "Src\Website\Website.csproj", "{A5573FE8-F544-4F18-B554-A8F9CC16A5DB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -109,6 +111,10 @@ Global
{497B849B-044C-4350-BDE8-13CA3E6C7845}.Debug|Any CPU.Build.0 = Debug|Any CPU
{497B849B-044C-4350-BDE8-13CA3E6C7845}.Release|Any CPU.ActiveCfg = Release|Any CPU
{497B849B-044C-4350-BDE8-13CA3E6C7845}.Release|Any CPU.Build.0 = Release|Any CPU
{A5573FE8-F544-4F18-B554-A8F9CC16A5DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A5573FE8-F544-4F18-B554-A8F9CC16A5DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5573FE8-F544-4F18-B554-A8F9CC16A5DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5573FE8-F544-4F18-B554-A8F9CC16A5DB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -128,5 +134,6 @@ Global
{3E1D074B-A4F3-4010-84E7-820459214202} = {0D12A95B-A237-4181-937C-DF6D0265975E}
{19874634-1A44-4831-BB43-6E2B864AB3ED} = {0D12A95B-A237-4181-937C-DF6D0265975E}
{0B075DD3-1FEE-483E-8665-43C226188287} = {0D12A95B-A237-4181-937C-DF6D0265975E}
{A5573FE8-F544-4F18-B554-A8F9CC16A5DB} = {0D12A95B-A237-4181-937C-DF6D0265975E}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion Src/CSharpier.Rider/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
id("org.jetbrains.intellij") version "1.3.0"
id("org.jetbrains.changelog") version "1.3.1"
id("org.jetbrains.qodana") version "0.1.13"
id("com.jetbrains.rdgen") version "0.213.394"
id("com.jetbrains.rdgen") version "2021.3.4"
}

group = properties("pluginGroup")
Expand Down
3 changes: 1 addition & 2 deletions Src/CSharpier.Rider/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

pluginGroup = com.intellij.csharpier
pluginName = csharpier
# SemVer format -> https://semver.org
pluginVersion = 1.3.10
pluginVersion = 1.5.0-beta1

# 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 @@ -9,16 +9,22 @@
public class CSharpierProcessPipeMultipleFiles implements ICSharpierProcess, Disposable {
private final boolean useUtf8;
private final String csharpierPath;
Logger logger = CSharpierLogger.getInstance();
private Logger logger = CSharpierLogger.getInstance();

Process process = null;
OutputStreamWriter stdin;
BufferedReader stdOut;
private Process process = null;
private OutputStreamWriter stdin;
private BufferedReader stdOut;
public boolean processFailedToStart;

public CSharpierProcessPipeMultipleFiles(String csharpierPath, boolean useUtf8) {
this.csharpierPath = csharpierPath;
this.useUtf8 = useUtf8;
this.startProcess();

this.logger.debug("Warm CSharpier with initial format");
// warm by formatting a file twice, the 3rd time is when it gets really fast
this.formatFile("public class ClassName { }", "Test.cs");
this.formatFile("public class ClassName { }", "Test.cs");
}

private void startProcess() {
Expand All @@ -35,22 +41,22 @@ private void startProcess() {
// 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);
this.logger.warn("Failed to spawn the needed csharpier process. Formatting cannot occur.", e);
this.processFailedToStart = true;
}

this.logger.debug("Warm CSharpier with initial format");
// warm by formatting a file twice, the 3rd time is when it gets really fast
this.formatFile("public class ClassName { }", "Test.cs");
this.formatFile("public class ClassName { }", "Test.cs");
}

@Override
public String formatFile(String content, String filePath) {
if (this.processFailedToStart) {
this.logger.warn("CSharpier proccess failed to start. Formatting cannot occur.");
return "";
}

var stringBuilder = new StringBuilder();

// TODO maybe pull in the retry stuff from here - https://github.com/belav/csharpier/commit/904bd14e1c028430a7b84571c320d1b54bf15500
Runnable task = () -> {
try {
this.stdin.write(filePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Map;

public class CSharpierProcessProvider implements DocumentListener, Disposable, IProcessKiller {
private final CustomPathInstaller customPathInstaller = new CustomPathInstaller();
private final CustomPathInstaller customPathInstaller;
private final Logger logger = CSharpierLogger.getInstance();
private final Project project;

Expand All @@ -38,6 +38,7 @@ public class CSharpierProcessProvider implements DocumentListener, Disposable, I

public CSharpierProcessProvider(@NotNull Project project) {
this.project = project;
this.customPathInstaller = new CustomPathInstaller(CSharpierSettings.getInstance(project));

for (var fileEditor : FileEditorManager.getInstance(project).getAllEditors()) {
var path = fileEditor.getFile().getPath();
Expand Down Expand Up @@ -200,7 +201,11 @@ private ICSharpierProcess setupCSharpierProcess(String directory, String version
}

try {
this.customPathInstaller.ensureVersionInstalled(version);
if (!this.customPathInstaller.ensureVersionInstalled(version)) {
this.displayFailureMessage();
return NullCSharpierProcess.Instance;
}

var customPath = this.customPathInstaller.getPathForVersion(version);

this.logger.debug("Adding new version " + version + " process for " + directory);
Expand All @@ -226,7 +231,11 @@ private ICSharpierProcess setupCSharpierProcess(String directory, String version

var useUtf8 = versionWeCareAbout >= 14;

return new CSharpierProcessPipeMultipleFiles(customPath, useUtf8);
// TODO I don't know if VSCode shows a message if the process failed to start
var csharpierProcess = new CSharpierProcessPipeMultipleFiles(customPath, useUtf8);
if (csharpierProcess.processFailedToStart) {
this.displayFailureMessage();
}

} catch (Exception ex) {
this.logger.error(ex);
Expand All @@ -235,6 +244,16 @@ private ICSharpierProcess setupCSharpierProcess(String directory, String version
return NullCSharpierProcess.Instance;
}

// TODO VSCode should get this helpful link too
private void displayFailureMessage() {
var title = "CSharpier unable to format files";
var message = "CSharpier could not be set up properly so formatting is not currently supported. See log file for more details.";
var notification = NotificationGroupManager.getInstance().getNotificationGroup("CSharpier")
.createNotification(title, message, NotificationType.WARNING);
notification.addAction(new OpenUrlAction("Read More","https://csharpier.com/docs/EditorsTroubleshooting"));
notification.notify(this.project);
}

@Override
public void dispose() {
this.killRunningProcesses();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public void setRunOnSave(boolean runOnSave) {
this.runOnSave = runOnSave;
}

private String customPath;

public String getCustomPath() {
return this.customPath;
}

public void setCustomPath(String customPath) {
this.customPath = customPath;
}

@Override
public CSharpierSettings getState() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public class CSharpierSettingsComponent implements SearchableConfigurable {
private final Project project;
private JCheckBox runOnSaveCheckBox = new JCheckBox("Run on Save");
private JTextField customPathTextField = new JTextField("Path to directory containing dotnet-csharpier - used for testing the extension with new versions of csharpier.");

public CSharpierSettingsComponent(@NotNull Project project) {
this.project = project;
Expand All @@ -34,25 +35,29 @@ public String getDisplayName() {
public @Nullable JComponent createComponent() {
return FormBuilder.createFormBuilder()
.addComponent(this.runOnSaveCheckBox)
.addComponent(this.customPathTextField)
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
}

@Override
public boolean isModified() {
return CSharpierSettings.getInstance(this.project).getRunOnSave() != this.runOnSaveCheckBox.isSelected();
return CSharpierSettings.getInstance(this.project).getRunOnSave() != this.runOnSaveCheckBox.isSelected()
|| CSharpierSettings.getInstance(this.project).getCustomPath() != this.customPathTextField.getText();
}

@Override
public void apply() throws ConfigurationException {
var settings = CSharpierSettings.getInstance(this.project);

settings.setRunOnSave(this.runOnSaveCheckBox.isSelected());
settings.setCustomPath(this.customPathTextField.getText());
}

@Override
public void reset() {
var settings = CSharpierSettings.getInstance(this.project);
this.runOnSaveCheckBox.setSelected(settings.getRunOnSave());
this.customPathTextField.setText(settings.getCustomPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,68 @@
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class CustomPathInstaller {
Logger logger = CSharpierLogger.getInstance();
String customPath;

public void ensureVersionInstalled(String version) throws Exception {
public CustomPathInstaller(CSharpierSettings settings) {
this.customPath = settings.getCustomPath();
}

public boolean ensureVersionInstalled(String version) throws Exception {
if (version == null || version.equals("")) {
return;
return true;
}
if (this.customPath != "" && this.customPath != null) {
this.logger.debug("Using csharpier at a custom path of " + this.customPath);
return true;
}

var pathToDirectoryForVersion = getDirectoryForVersion(version);
var directoryForVersion = new File(pathToDirectoryForVersion);
if (directoryForVersion.exists()) {
try {
Map<String, String> env = new HashMap<>();
env.put("DOTNET_NOLOGO", "1");

var command = new String[] { getPathForVersion(version), "--version" };
var output = ProcessHelper.ExecuteCommand(command, env, new File(pathToDirectoryForVersion));

this.logger.debug("dotnet csharpier --version output: " + output);

if (output.equals(version))
{
this.logger.debug("CSharpier at " + pathToDirectoryForVersion + " already exists");
return;
}
}
catch (Exception ex) {
// TODO somehow I got a bunch of the versions to install that were missing dotnet-csharpier in the root of the custom path
// this needs to do a better job of figuring that out and reporting it.
// I think when this fails to install, then the other stuff gets stuck in an infinite loop
logger.warn("Exception while running 'dotnet csharpier --version' in " + pathToDirectoryForVersion, ex);
if (this.validateInstall(pathToDirectoryForVersion, version)) {
return true;
}

// if we got here something isn't right in the current directory
this.logger.debug(
"Removing directory at " + pathToDirectoryForVersion + " because it appears to be corrupted"
);
deleteDirectory(directoryForVersion);
}

var command = new String[]{"dotnet", "tool", "install", "csharpier", "--version", version, "--tool-path", pathToDirectoryForVersion};
ProcessHelper.ExecuteCommand(command, null, null);

return this.validateInstall(pathToDirectoryForVersion, version);
}

private boolean validateInstall(String pathToDirectoryForVersion, String version) {
try {
Map<String, String> env = new HashMap<>();
env.put("DOTNET_NOLOGO", "1");

var command = new String[] { this.getPathForVersion(version), "--version" };
var output = ProcessHelper.ExecuteCommand(command, env, new File(pathToDirectoryForVersion)).trim();

this.logger.debug("dotnet csharpier --version output: " + output);

if (output.split(Pattern.quote("+"))[0].equals(version))
{
this.logger.debug("CSharpier at " + pathToDirectoryForVersion + " already exists");
return true;
}
}
catch (Exception ex) {
this.logger.warn("Exception while running 'dotnet csharpier --version' in " + pathToDirectoryForVersion, ex);
}

return false;
}

boolean deleteDirectory(File directoryToBeDeleted) {
private boolean deleteDirectory(File directoryToBeDeleted) {
File[] allContents = directoryToBeDeleted.listFiles();
if (allContents != null) {
for (File file : allContents) {
Expand All @@ -59,6 +79,10 @@ boolean deleteDirectory(File directoryToBeDeleted) {
}

private String getDirectoryForVersion(String version) throws Exception {
if (this.customPath != "" && this.customPath != null) {
return this.customPath;
}

if (SystemUtils.IS_OS_WINDOWS) {
return Path.of(System.getenv("LOCALAPPDATA"), "CSharpier", version).toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.intellij.csharpier;

import com.intellij.ide.BrowserUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import org.jetbrains.annotations.NotNull;

public class OpenUrlAction extends AnAction {
private String url;

public OpenUrlAction(String title, String url) {
super(title);
this.url = url;
}

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
BrowserUtil.browse(this.url);
}
}
24 changes: 24 additions & 0 deletions Src/CSharpier/CodeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,32 @@ public static class CodeFormatter
{
public static CodeFormatterResult Format(string code, CodeFormatterOptions? options = null)
{



return FormatAsync(code, options).Result;
}






















public static Task<CodeFormatterResult> FormatAsync(
string code,
Expand Down
2 changes: 2 additions & 0 deletions Src/CSharpier/CodeFormatterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ namespace CSharpier;

public class CodeFormatterOptions
{


public int Width { get; init; } = 100;
}
6 changes: 6 additions & 0 deletions Src/CSharpier/GeneratedCodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ public static bool IsGeneratedCodeFile(string filePath)
var fileName = Path.GetFileName(filePath);
if (fileName.StartsWithIgnoreCase("TemporaryGeneratedFile_"))
{






return true;
}

Expand Down
Loading

0 comments on commit cc2e389

Please sign in to comment.