Skip to content

Commit

Permalink
Format code and remove unused imports
Browse files Browse the repository at this point in the history
Refactored code to improve readability by reformatting long lines and adding line breaks. Also, removed unnecessary imports and cleaned up redundant code, enhancing overall maintainability.
  • Loading branch information
ignatov committed Sep 30, 2024
1 parent c196fc6 commit 6f080f2
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ProcessingContext;
import com.intellij.util.containers.ContainerUtil;
import org.intellij.erlang.ErlangLanguage;
import org.intellij.erlang.parser.ErlangLexer;
import org.intellij.erlang.parser.ErlangParserUtil;
Expand Down Expand Up @@ -102,9 +101,9 @@ private static LookupElement createKeywordLookupElement(@NotNull String keyword)
boolean needBrackets = "export".equalsIgnoreCase(keyword) || "export_type".equalsIgnoreCase(keyword) ||
"optional_callbacks".equalsIgnoreCase(keyword);
return PrioritizedLookupElement.withPriority(LookupElementBuilder.create(keyword)
.withInsertHandler(needHandler ? new ErlangKeywordInsertHandler(needQuotas, needBrackets) : null)
.withTailText(needHandler ? "()" : null)
.bold(), ErlangCompletionContributor.KEYWORD_PRIORITY);
.withInsertHandler(needHandler ? new ErlangKeywordInsertHandler(needQuotas, needBrackets) : null)
.withTailText(needHandler ? "()" : null)
.bold(), ErlangCompletionContributor.KEYWORD_PRIORITY);
}

private static class ErlangKeywordInsertHandler extends ParenthesesInsertHandler<LookupElement> {
Expand Down Expand Up @@ -135,7 +134,10 @@ public void handleInsert(@NotNull InsertionContext context, @NotNull LookupEleme
if (insertBrackets()) doInsert(editor, document, "[", "]");
}

private static void doInsert(@NotNull Editor editor, @NotNull Document document, @NotNull String open, @NotNull String closed) {
private static void doInsert(@NotNull Editor editor,
@NotNull Document document,
@NotNull String open,
@NotNull String closed) {
int offset = editor.getCaretModel().getOffset();
document.insertString(offset, open);
document.insertString(offset + 1, closed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public final class ErlangConsoleRunConfigurationForm extends SettingsEditor<Erla
private TextFieldWithBrowseButton myWorkingDirPathField;
private JComboBox<Module> myModuleComboBox;

@Nullable private final Module myInitialModule;
@Nullable
private final Module myInitialModule;

public ErlangConsoleRunConfigurationForm(@NotNull Project project, @Nullable Module module) {
myInitialModule = module;
Expand Down Expand Up @@ -81,9 +82,13 @@ protected void disposeEditor() {

@NotNull
private static SimpleListCellRenderer<Module> getListCellRendererWrapper() {
return new SimpleListCellRenderer<Module>() {
return new SimpleListCellRenderer<>() {
@Override
public void customize(@NotNull JList<? extends Module> list, @Nullable Module module, int index, boolean selected, boolean hasFocus) {
public void customize(@NotNull JList<? extends Module> list,
@Nullable Module module,
int index,
boolean selected,
boolean hasFocus) {
if (module != null) {
setText(module.getName());
}
Expand Down
57 changes: 27 additions & 30 deletions src/org/intellij/erlang/editor/ErlangColorSettingsPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

import static org.intellij.erlang.editor.ErlangSyntaxHighlighter.*;
Expand Down Expand Up @@ -55,27 +54,25 @@ public class ErlangColorSettingsPage implements ColorSettingsPage {
new AttributesDescriptor("Guards", GUARD),
new AttributesDescriptor("Callbacks", CALLBACK),
new AttributesDescriptor("Specifications", SPEC),
};
};

private static final Map<String, TextAttributesKey> ATTRIBUTES_KEY_MAP = Map.ofEntries(
Map.entry("a", ATOM),
Map.entry("d", DOC_TAG),
Map.entry("k", KEYWORD),
Map.entry("m", MACRO),
Map.entry("r", RECORDS),
Map.entry("f", FUNCTION),
Map.entry("t", TYPE),
Map.entry("bt", BUILT_IN_TYPE),
Map.entry("m_att", ATTRIBUTE),
Map.entry("c", FUNCTION_CALL),
Map.entry("mr", MODULE_REF),
Map.entry("g", GUARD),
Map.entry("s", SPEC),
Map.entry("cb", CALLBACK)
);

private static final Map<String, TextAttributesKey> ATTRIBUTES_KEY_MAP = new HashMap<>();

static {
ATTRIBUTES_KEY_MAP.put("a", ATOM);
ATTRIBUTES_KEY_MAP.put("d", DOC_TAG);
ATTRIBUTES_KEY_MAP.put("k", KEYWORD);
ATTRIBUTES_KEY_MAP.put("m", MACRO);
ATTRIBUTES_KEY_MAP.put("r", RECORDS);
ATTRIBUTES_KEY_MAP.put("f", FUNCTION);
ATTRIBUTES_KEY_MAP.put("t", TYPE);
ATTRIBUTES_KEY_MAP.put("bt", BUILT_IN_TYPE);
ATTRIBUTES_KEY_MAP.put("m_att", ATTRIBUTE);
ATTRIBUTES_KEY_MAP.put("c", FUNCTION_CALL);
ATTRIBUTES_KEY_MAP.put("mr", MODULE_REF);
ATTRIBUTES_KEY_MAP.put("g", GUARD);
ATTRIBUTES_KEY_MAP.put("s", SPEC);
ATTRIBUTES_KEY_MAP.put("cb", CALLBACK);
}

@NotNull
public String getDisplayName() {
return "Erlang";
Expand Down Expand Up @@ -106,29 +103,29 @@ public String getDemoText() {
%%% Module fact documentation
<m_att>-module</m_att>(fact).
<m_att>-export</m_att>([<f>fac</f>/1]).
<m_att>-record</m_att>(<r>state</r>, {id, name}).
<m_att>-define</m_att>(<m>MACRO</m>, macro_value).
<m_att>-type</m_att> <t>in</t>() :: ok | hello .
<m_att>-type</m_att> <t>out</t>() :: ok | {error, <bt>term</bt>()}.
%% Factorial implementation
%% <d>@doc</d> Documentation
<f>fac</f>(0) -> 1;
<f>fac</f>(N) when N > 0, <g>is_integer</g>(N) -> N * <c>fac</c>(N-1).
<f>string_sample</f>(A) -> "string
second line".
<f>update_state</f>(State) -> State#<r>state</r>{id=10}.
<m_att>-spec</m_att> <s>simple</s>(<t>in</t>())-> <t>out</t>().\s
<f>simple</f>(<a>ok</a>) -> <a>ok</a>.
<f>use_macro</f>() -> <mr>io</mr>:<c>format</c>(?<m>MACRO</m>).
<m_att>-callback</m_att> <cb>start_service</cb>() -> {<a>ok</a>, <bt>pid</bt>()}."""
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.intellij.erlang.editor;

import com.intellij.openapi.fileTypes.SingleLazyInstanceSyntaxHighlighterFactory;
import com.intellij.openapi.fileTypes.SyntaxHighlighter;
import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory;
import com.intellij.openapi.project.Project;
Expand Down
11 changes: 6 additions & 5 deletions src/org/intellij/erlang/eunit/ErlangUnitRunningState.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public List<String> getCodePath() throws ExecutionException {
try {
List<String> reporterModuleCodePath = Arrays.asList("-pa", createReporterModuleDirectory());
return ContainerUtil.concat(reporterModuleCodePath, super.getCodePath());
} catch (IOException e) {
}
catch (IOException e) {
throw new ExecutionException("Failed to setup eunit reports environment", e);
}
}
Expand Down Expand Up @@ -143,14 +144,14 @@ else if (kind == ErlangUnitRunConfiguration.ErlangUnitRunConfigurationKind.FUNCT
for (String function : e.getValue()) {
boolean isGenerator = ErlangPsiImplUtil.isEunitTestGeneratorFunctionName(function);
result.append(isGenerator ? "{generator, " : "")
.append("fun ").append(moduleName).append(':').append(function).append("/0")
.append(isGenerator ? "}" : "")
.append(", ");
.append("fun ").append(moduleName).append(':').append(function).append("/0")
.append(isGenerator ? "}" : "")
.append(", ");
}
result.setLength(result.length() - 2);
result.append("]}, ");
}
if (result.length() != 0) {
if (!result.isEmpty()) {
result.setLength(result.length() - 2);
}
tests = result.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
import org.jetbrains.annotations.NotNull;

public class ErlangUnitTestMethodAction extends CodeInsightAction implements CodeInsightActionHandler {
private static void insertTestFunction(@NotNull Project project, @NotNull Editor editor, String name, boolean needNewline) {
private static void insertTestFunction(@NotNull Project project,
@NotNull Editor editor,
String name,
boolean needNewline) {
Template template = TemplateManager.getInstance(project).createTemplate("", "");
Expression nameExpr = new ConstantNode(name);
Expression expected = new ConstantNode("expected");
Expand Down Expand Up @@ -75,11 +78,6 @@ public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull Ps
insertTestFunction(project, editor, name, needNewline);
}

@Override
public boolean startInWriteAction() {
return true;
}

@Override
protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
if (!(file instanceof ErlangFile)) return false;
Expand Down
8 changes: 3 additions & 5 deletions src/org/intellij/erlang/navigation/ErlangNavigationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private ErlangNavigationUtil() {
@NotNull
public static List<ErlangCallbackSpec> getCallbackSpecs(@NotNull ErlangFunction function) {
final String functionPresentation = ErlangPsiImplUtil.createFunctionPresentation(function);
return ContainerUtil.mapNotNull(((ErlangFile)function.getContainingFile()).getBehaviours(), behaviour -> {
return ContainerUtil.mapNotNull(((ErlangFile) function.getContainingFile()).getBehaviours(), behaviour -> {
ErlangFile behaviourModule = ErlangPsiImplUtil.resolveToFile(behaviour.getModuleRef());
return behaviourModule != null ? behaviourModule.getCallbackByName(functionPresentation) : null;
});
Expand Down Expand Up @@ -70,17 +70,15 @@ public String getLocationString() {
return getContainingFile().getName();
}

@Nullable
@Override
public Icon getIcon(boolean unused) {
public @NotNull Icon getIcon(boolean unused) {
return ErlangIcons.CALLBACK;
}
};
}

@Nullable
@Override
public Icon getIcon(int flags) {
public @NotNull Icon getIcon(int flags) {
return ErlangIcons.CALLBACK;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public int getDefaultOptionIndex() {
@NotNull
@Override
public String getTitle() {
return "Multiple files found";
return "Multiple Files Found";
}

@Nullable
Expand All @@ -169,7 +169,7 @@ public PopupStep<PsiFile> onChosen(PsiFile o, boolean b) {
fixUsingIncludeFile(problem, o);
renameIncludeString(project, problem, setDirectHrlLink, includeString, includeFileName);
FileContentUtilCore.reparseFiles(Collections.singletonList(problem.getContainingFile().getVirtualFile()));
}), "add facet action(find include quick fix)", null, problemEditor.getDocument());
}), "Add Facet Action (Find Include Quick Fix)", null, problemEditor.getDocument());

return null;
}
Expand Down Expand Up @@ -228,13 +228,13 @@ private static void fixUsingIncludeFile(PsiElement problem,
}

/*
* returns file name from includeString
* eg:
* getFileName("pr285_helper/include/internal_communication.hrl")
* -> "internal_communications.hrl"
* getFileName("ecst_events.hrl")
* -> "ecst_events.hrl"
* */
* returns file name from includeString
* eg:
* getFileName("pr285_helper/include/internal_communication.hrl")
* -> "internal_communications.hrl"
* getFileName("ecst_events.hrl")
* -> "ecst_events.hrl"
* */
private static String getFileName(String includeString) {
int index = includeString.lastIndexOf(INCLUDE_STRING_PATH_SEPARATOR);
return includeString.substring(index + 1);
Expand Down
4 changes: 2 additions & 2 deletions src/org/intellij/erlang/quickfixes/ErlangGenerateSpecFix.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@

public class ErlangGenerateSpecFix extends ErlangQuickFixBase {
public static final String NAME = "Generate function spec";
private static final String ANY_TYPE_STRING = "any()";

@NotNull
@Override
Expand All @@ -60,6 +59,7 @@ public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descri

/**
* Try and guess variable name used in the given expression, for spec argument naming purpose.
*
* @return The guess or null
*/
private static @Nullable String guessArgumentName(ErlangCompositeElement expression) {
Expand All @@ -72,7 +72,7 @@ public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descri

if (recordRef != null) {
var RecordName = recordRef.getQAtom().getText();
return RecordName.length() > 0
return !RecordName.isEmpty()
? RecordName.substring(0, 1).toUpperCase() + RecordName.substring(1)
: null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiEditorUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtilBase;
import org.intellij.erlang.psi.ErlangFile;
import org.intellij.erlang.psi.ErlangMacros;
import org.intellij.erlang.psi.ErlangMacrosDefinition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descri
StringBuilder newExport = new StringBuilder();
for (ErlangExportFunction ef : exportFunctions.getExportFunctionList()) {
if (ef == function || !ef.getText().equals(function.getText())) {
if (newExport.length() > 0)
if (!newExport.isEmpty()) {
newExport.append(", ");
}
newExport.append(ef.getText());
}
}
if (newExport.length() == 0) {
if (attribute.getNextSibling() instanceof PsiWhiteSpace)
if (newExport.isEmpty()) {
if (attribute.getNextSibling() instanceof PsiWhiteSpace) {
attribute.getNextSibling().delete();
}
attribute.delete();
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public CustomShortcutSet getMarkRootShortcutSet() {
@NotNull
@Override
public String getRootsGroupTitle() {
return "Include directories";
return "Include Directories";
}

@NotNull
Expand All @@ -72,6 +72,6 @@ public Color getRootsGroupColor() {
@NotNull
@Override
public String getUnmarkRootButtonText() {
return "Unmark include directory";
return "Unmark Include Directory";
}
}

0 comments on commit 6f080f2

Please sign in to comment.