Skip to content

Commit

Permalink
Fixed ruff integration to properly set the format for versions >= 0.1…
Browse files Browse the repository at this point in the history
….0 (RUFF_OUTPUT_FORMAT is set as environment variable).
  • Loading branch information
fabioz committed Jan 20, 2024
1 parent 3feac83 commit 0267bdd
Showing 1 changed file with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.python.pydev.json.eclipsesource.JsonObject;
import org.python.pydev.json.eclipsesource.JsonValue;
import org.python.pydev.plugin.nature.PythonNature;
import org.python.pydev.shared_core.callbacks.ICallback;
import org.python.pydev.shared_core.callbacks.ICallback0;
import org.python.pydev.shared_core.io.FileUtils;
import org.python.pydev.shared_core.markers.PyMarkerUtils;
Expand All @@ -46,6 +47,7 @@
import org.python.pydev.shared_core.string.FastStringBuffer;
import org.python.pydev.shared_core.string.StringUtils;
import org.python.pydev.shared_core.structure.Tuple;
import org.python.pydev.shared_core.utils.ArrayUtils;

import com.python.pydev.analysis.external.ExternalAnalizerProcessWatchDoc;
import com.python.pydev.analysis.external.IExternalAnalyzer;
Expand Down Expand Up @@ -152,9 +154,6 @@ void createRuffProcess(IExternalCodeAnalysisStream out)
String userArgs = StringUtils.replaceNewLines(
RuffPreferences.getRuffArgs(resource), " ");
List<String> userArgsAsList = new ArrayList<>(Arrays.asList(ProcessUtils.parseArguments(userArgs)));
if (!userArgsAsList.contains("--format=json")) {
userArgsAsList.add("--format=json");
}
// run ruff in project location
IProject project = resource.getProject();
if (project == null || !project.isAccessible()) {
Expand All @@ -170,6 +169,25 @@ void createRuffProcess(IExternalCodeAnalysisStream out)

IPythonNature nature = PythonNature.getPythonNature(project);

ICallback<String[], String[]> updateEnv = new ICallback<String[], String[]>() {

@Override
public String[] call(String[] arg) {
for (int i = 0; i < arg.length; i++) {
// Update var
if (arg[i].startsWith("RUFF_FORMAT=")) {
arg[i] = "RUFF_FORMAT=json";
} else if (arg[i].startsWith("RUFF_OUTPUT_FORMAT=")) {
arg[i] = "RUFF_OUTPUT_FORMAT=json";
}
}

// Duplicating should be Ok
return ArrayUtils.concatArrays(arg, new String[] {
"RUFF_FORMAT=json", "RUFF_OUTPUT_FORMAT=json" });
}
};

ICallback0<Process> launchProcessCallback;
if (ruffLocation == null) {
// use python -m ruff
Expand All @@ -187,7 +205,7 @@ void createRuffProcess(IExternalCodeAnalysisStream out)
SimplePythonRunner runner = new SimplePythonRunner();
String[] parameters = SimplePythonRunner.preparePythonCallParameters(interpreter, "-m", args);

Tuple<Process, String> r = runner.run(parameters, workingDir, nature, monitor, null);
Tuple<Process, String> r = runner.run(parameters, workingDir, nature, monitor, updateEnv);
return r.o1;
};

Expand All @@ -203,7 +221,7 @@ void createRuffProcess(IExternalCodeAnalysisStream out)

SimpleRunner simpleRunner = new SimpleRunner();
final Tuple<Process, String> r = simpleRunner.run(args, workingDir, nature,
null, null);
null, updateEnv);
Process process = r.o1;
return process;
};
Expand Down

0 comments on commit 0267bdd

Please sign in to comment.