Skip to content
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

Feature/37 #38

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions gui/src/main/java/tech/hiddenproject/compaj/gui/AppPreference.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package tech.hiddenproject.compaj.gui;

/**
* Contains app preferences.
*/
public enum AppPreference {
LANGUAGE("LANG");
/**
* App language
*/
LANGUAGE("LANG"),
/**
* Code completion feature.
*/
CODE_AUTOCOMPLETE("CODE_AUTOCOMPLETE");

private String name;
private final String name;

AppPreference(String name) {
this.name = name;
Expand All @@ -12,4 +22,21 @@ public enum AppPreference {
public String getName() {
return name;
}

public String getOrDefault(String defaultValue) {
return AppSettings.getInstance().get(this, defaultValue);
}

public Boolean getOrDefault(Boolean defaultValue) {
return Boolean.valueOf(AppSettings.getInstance()
.get(this, String.valueOf(defaultValue)));
}

public void update(String value) {
AppSettings.getInstance().put(this, value);
}

public void update(Boolean value) {
AppSettings.getInstance().put(this, String.valueOf(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,18 @@ public void put(AppPreference preference, String value) {
preferences.put(preference.getName(), value);
}

public void put(AppPreference preference, Boolean value) {
preferences.put(preference.getName(), String.valueOf(value));
}

public String get(AppPreference preference, String defaultValue) {
return preferences.get(preference.getName(), defaultValue);
}

public Boolean get(AppPreference preference, Boolean defaultValue) {
return Boolean.valueOf(preferences.get(preference.getName(), String.valueOf(defaultValue)));
}

public FileFilter pluginsFileFilter() {
return pathname -> pathname.getName().endsWith(".jar");
}
Expand Down
8 changes: 8 additions & 0 deletions gui/src/main/java/tech/hiddenproject/compaj/gui/Compaj.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,16 @@ public void start(Stage stage) {
Menu helpMenu = new Menu(I18nUtils.get("menu.help"));
Menu settingsMenu = new Menu(I18nUtils.get("menu.settings"));

Menu terminalSettingsMenu = new Menu(I18nUtils.get("tab.terminal.title"));
Menu editorSettingsMenu = new Menu(I18nUtils.get("tab.editor.title"));
Menu workSpaceSettingsMenu = new Menu(I18nUtils.get("tab.workspace.title"));
settingsMenu.getItems().addAll(terminalSettingsMenu, editorSettingsMenu, workSpaceSettingsMenu);

ROOT_MENUS.put("menu.help", helpMenu);
ROOT_MENUS.put("menu.settings", settingsMenu);
ROOT_MENUS.put("menu.settings.terminal", terminalSettingsMenu);
ROOT_MENUS.put("menu.settings.editor", editorSettingsMenu);
ROOT_MENUS.put("menu.settings.workspace", workSpaceSettingsMenu);

menuBar = new MenuBar();
final String os = System.getProperty("os.name");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public ContextMenuBuilder() {
contextMenu = new ContextMenu();
}

public ContextMenuBuilder(ContextMenu contextMenu) {
this.contextMenu = contextMenu;
}

public ContextMenuBuilder add(String name, Consumer<MenuItem> event) {
MenuItem menuItem = new MenuItem(name);
menuItem.setOnAction(actionEvent -> event.accept(menuItem));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package tech.hiddenproject.compaj.gui.plugin;

import javafx.scene.control.MenuItem;
import tech.hiddenproject.compaj.gui.AppPreference;
import tech.hiddenproject.compaj.gui.event.UiChildPayload;
import tech.hiddenproject.compaj.gui.event.UiMenuEvent;
import tech.hiddenproject.compaj.gui.util.I18nUtils;
import tech.hiddenproject.compaj.plugin.api.CompaJPlugin;
import tech.hiddenproject.compaj.plugin.api.event.CompaJEvent;
import tech.hiddenproject.compaj.plugin.api.event.EventPublisher;

public class AutoCompletePlugin implements CompaJPlugin {

public AutoCompletePlugin() {
EventPublisher.INSTANCE.subscribeOn(UiMenuEvent.STARTUP_NAME, this::addSettingsOnStartup);
}

private void addSettingsOnStartup(CompaJEvent compaJEvent) {
MenuItem codeCompletion = new MenuItem();
updateCodeCompletionStateText(AppPreference.CODE_AUTOCOMPLETE.getOrDefault(true),
codeCompletion);
codeCompletion.setOnAction(actionEvent -> {
Boolean isEnabled = AppPreference.CODE_AUTOCOMPLETE.getOrDefault(true);
updateCodeCompletionStateText(!isEnabled, codeCompletion);
AppPreference.CODE_AUTOCOMPLETE.update(!isEnabled);
});
UiChildPayload autoCompleteSettings = new UiChildPayload("menu.settings.editor",
codeCompletion);
EventPublisher.INSTANCE.sendTo(UiMenuEvent.ADD_CHILD(autoCompleteSettings));
}

private void updateCodeCompletionStateText(Boolean isEnabled, MenuItem menuItem) {
if (isEnabled) {
menuItem.setText(I18nUtils.get("menu.settings.editor.autocomplete.disable"));
} else {
menuItem.setText(I18nUtils.get("menu.settings.editor.autocomplete.enable"));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tech.hiddenproject.compaj.gui.suggestion;

import java.util.Set;
import java.util.stream.Collectors;

/**
* {@link Suggester} for keywords
*/
public class KeywordSuggester implements Suggester {

public static final Set<String> KEYWORDS =
Set.of(
"abstract", "assert", "boolean", "break", "byte",
"case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else",
"enum", "extends", "final", "finally", "float",
"for", "goto", "if", "implements", "import",
"instanceof", "int", "interface", "long", "native",
"new", "package", "private", "protected", "public",
"return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws",
"transient", "try", "void", "volatile", "while", "trait"
);

@Override
public Set<String> predict(String input, String prefix, int position) {
return KEYWORDS.stream()
.filter(keyword -> keyword.startsWith(prefix))
.collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package tech.hiddenproject.compaj.gui.suggestion;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Creates suggestions for given text.
*/
public class SuggestCore {

private final List<Suggester> suggesters = new ArrayList<>();

/**
* @param suggester {@link Suggester}
*/
public void addSuggester(Suggester suggester) {
this.suggesters.add(suggester);
}

/**
* Collects suggestions from all {@link Suggester}.
*
* @param text Text to suggestions are for
* @param position Current caret position in text
* @return {@link SuggestResult}
*/
public SuggestResult predict(String text, int position) {
String preparedText = text.replace("\n", " ");
int lastSpace = preparedText.substring(0, position).lastIndexOf(" ");
String prefix = (lastSpace > -1 ? preparedText.substring(lastSpace + 1, position)
: preparedText).trim();
Set<String> suggestions = suggesters.stream()
.flatMap(suggester -> suggester.predict(preparedText, prefix, position).stream())
.collect(Collectors.toSet());
return new SuggestResult(suggestions, prefix);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tech.hiddenproject.compaj.gui.suggestion;

import java.util.Set;

/**
* Result of {@link SuggestCore}.
*/
public class SuggestResult {

private final Set<String> suggestions;
private final String prefix;

public SuggestResult(Set<String> suggestions, String prefix) {
this.suggestions = suggestions;
this.prefix = prefix;
}

/**
* @return Found suggestions
*/
public Set<String> getSuggestions() {
return suggestions;
}

/**
* @return Prefix for which suggestions are for
*/
public String getPrefix() {
return prefix;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tech.hiddenproject.compaj.gui.suggestion;

import java.util.Set;

public interface Suggester {

Set<String> predict(String text, String prefix, int position);

}
Loading