-
-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Formatting based on external formatter #80
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e2d42bc
Formatting based on external formatter
cottand 794f80a
satisfy linter
cottand b25a9eb
remove unused import
cottand 8074155
address review: feed file contents async
cottand 791f5b4
remove src/gen
cottand 8d08642
address review
cottand 0a67c4f
address review
cottand cb81940
rename stateful OS-dependent lang settings to NixExternalFormatterSet…
cottand b3c2898
remove unused bundle id in plugin.xml
cottand 68d26f1
add formatting to changelog
cottand bf1c923
Some adjustments after review
JojOatXGME File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,5 @@ hs_err_pid* | |
*.iml | ||
**/.gradle | ||
build | ||
|
||
src/gen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/main/java/org/nixos/idea/format/NixExternalFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package org.nixos.idea.format; | ||
|
||
import com.intellij.execution.ExecutionException; | ||
import com.intellij.execution.configurations.GeneralCommandLine; | ||
import com.intellij.execution.process.CapturingProcessAdapter; | ||
import com.intellij.execution.process.OSProcessHandler; | ||
import com.intellij.execution.process.ProcessEvent; | ||
import com.intellij.formatting.service.AsyncDocumentFormattingService; | ||
import com.intellij.formatting.service.AsyncFormattingRequest; | ||
import com.intellij.openapi.util.NlsSafe; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.util.execution.ParametersListUtil; | ||
import org.jetbrains.annotations.NonNls; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.nixos.idea.file.NixFile; | ||
import org.nixos.idea.lang.NixLanguage; | ||
import org.nixos.idea.settings.NixExternalFormatterSettings; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.util.EnumSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public final class NixExternalFormatter extends AsyncDocumentFormattingService { | ||
|
||
@Override | ||
protected @NotNull String getNotificationGroupId() { | ||
return NixLanguage.NOTIFICATION_GROUP_ID; | ||
} | ||
|
||
@Override | ||
protected @NotNull @NlsSafe String getName() { | ||
return "NixIDEA"; | ||
} | ||
|
||
@Override | ||
public @NotNull Set<Feature> getFeatures() { | ||
return EnumSet.noneOf(Feature.class); | ||
} | ||
|
||
@Override | ||
public boolean canFormat(@NotNull PsiFile psiFile) { | ||
return psiFile instanceof NixFile; | ||
} | ||
|
||
|
||
@Override | ||
protected @Nullable FormattingTask createFormattingTask(@NotNull AsyncFormattingRequest request) { | ||
NixExternalFormatterSettings nixSettings = NixExternalFormatterSettings.getInstance(); | ||
if (!nixSettings.isFormatEnabled()) { | ||
return null; | ||
} | ||
|
||
var ioFile = request.getIOFile(); | ||
if (ioFile == null) return null; | ||
|
||
@NonNls | ||
var command = nixSettings.getFormatCommand(); | ||
List<String> argv = ParametersListUtil.parse(command, false, true); | ||
|
||
var commandLine = new GeneralCommandLine(argv); | ||
|
||
try { | ||
var handler = new OSProcessHandler(commandLine.withCharset(StandardCharsets.UTF_8)); | ||
OutputStream processInput = handler.getProcessInput(); | ||
return new FormattingTask() { | ||
@Override | ||
public void run() { | ||
handler.addProcessListener(new CapturingProcessAdapter() { | ||
|
||
@Override | ||
public void processTerminated(@NotNull ProcessEvent event) { | ||
int exitCode = event.getExitCode(); | ||
if (exitCode == 0) { | ||
request.onTextReady(getOutput().getStdout()); | ||
} else { | ||
request.onError("NixIDEA", getOutput().getStderr()); | ||
} | ||
} | ||
}); | ||
handler.startNotify(); | ||
try { | ||
Files.copy(ioFile.toPath(), processInput); | ||
processInput.flush(); | ||
processInput.close(); | ||
} catch (IOException e) { | ||
handler.destroyProcess(); | ||
request.onError("NixIDEA", e.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean cancel() { | ||
handler.destroyProcess(); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isRunUnderProgress() { | ||
return true; | ||
} | ||
}; | ||
} catch (ExecutionException e) { | ||
request.onError("NixIDEA", e.getMessage()); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/org/nixos/idea/settings/NixExternalFormatterSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.nixos.idea.settings; | ||
|
||
import com.intellij.openapi.application.ApplicationManager; | ||
import com.intellij.openapi.components.PersistentStateComponent; | ||
import com.intellij.openapi.components.RoamingType; | ||
import com.intellij.openapi.components.State; | ||
import com.intellij.openapi.components.Storage; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Deque; | ||
|
||
@State(name = "NixExternalFormatterSettings", storages = @Storage(value = NixStoragePaths.TOOLS, roamingType = RoamingType.DISABLED)) | ||
public final class NixExternalFormatterSettings implements PersistentStateComponent<NixExternalFormatterSettings.State> { | ||
|
||
// Documentation: | ||
// https://plugins.jetbrains.com/docs/intellij/persisting-state-of-components.html | ||
|
||
private static final int MAX_HISTORY_SIZE = 5; | ||
|
||
private @NotNull State myState = new State(); | ||
|
||
public static @NotNull NixExternalFormatterSettings getInstance() { | ||
return ApplicationManager.getApplication().getService(NixExternalFormatterSettings.class); | ||
} | ||
|
||
public boolean isFormatEnabled() { | ||
return myState.enabled; | ||
} | ||
|
||
public void setFormatEnabled(boolean enabled) { | ||
myState.enabled = enabled; | ||
} | ||
|
||
public @NotNull String getFormatCommand() { | ||
return myState.command; | ||
} | ||
|
||
public void setFormatCommand(@NotNull String command) { | ||
myState.command = command; | ||
addFormatCommandToHistory(command); | ||
} | ||
|
||
public @NotNull Collection<String> getCommandHistory() { | ||
return Collections.unmodifiableCollection(myState.history); | ||
} | ||
|
||
private void addFormatCommandToHistory(@NotNull String command) { | ||
Deque<String> history = myState.history; | ||
history.remove(command); | ||
history.addFirst(command); | ||
while (history.size() > MAX_HISTORY_SIZE) { | ||
history.removeLast(); | ||
} | ||
} | ||
|
||
@SuppressWarnings("ClassEscapesDefinedScope") | ||
@Override | ||
public void loadState(@NotNull State state) { | ||
myState = state; | ||
} | ||
|
||
@SuppressWarnings("ClassEscapesDefinedScope") | ||
@Override | ||
public @NotNull State getState() { | ||
return myState; | ||
} | ||
|
||
static final class State { | ||
public boolean enabled = false; | ||
public @NotNull String command = ""; | ||
public Deque<String> history = new ArrayDeque<>(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question (non-blocking): Should we maybe use “Nix External Formatter” for the title? (Same in catch block.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is clearer to use the plugin name as title, so it is clear who is 'to blame' for the error, but I do not have a strong opinion.
I can change it to Nix External Formatter if you prefer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am fine with both. I think both have their pros and cons. 😅