Automate switching between languages

This commit is contained in:
2021-02-23 00:46:43 +01:00
parent 1860d4c57f
commit 4e24363e63
14 changed files with 418 additions and 74 deletions

View File

@@ -5,6 +5,7 @@ import java.util.Collection;
import java.util.ResourceBundle;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;
@@ -13,6 +14,7 @@ import org.fxmisc.richtext.model.TwoDimensional.Bias;
import org.fxmisc.richtext.model.TwoDimensional.Position;
import app.events.EditorChangedEvent;
import app.events.LanguageChangedEvent;
import app.model.Model;
import app.service.LanguageOperations;
import javafx.fxml.FXML;
@@ -39,20 +41,26 @@ public class EditorController implements Initializable, Controller {
}
/**
* Links the controller to the global model
* @param model The model to be linked
* Applies highlighting to the editor.
* @param highlighting highlighting data
*/
public void setModel(Model model) {
this.model = model;
}
public void setHighlighting(StyleSpans<Collection<String>> highlighting) {
private void setHighlighting(StyleSpans<Collection<String>> highlighting) {
this.editor.setStyleSpans(0, highlighting);
}
/**
* Handles events from the editor, and reflects them in the model
* @param event The object containing metadata of the event
* Recalculates and refreshes the syntax highlighting of the editor.
*/
private void refreshHighlighting() {
this.setHighlighting(
LanguageOperations.syntaxHighlight(
this.editor.getText(),
Model.getLanguage()));
}
/**
* Handles the event whenever the content of the editor is changed.
* @param event JavaFX event containing metadata
*/
@FXML
public void editorChanged(KeyEvent event) {
@@ -64,10 +72,16 @@ public class EditorController implements Initializable, Controller {
pos.getMajor() + 1,
pos.getMinor()
));
this.setHighlighting(
LanguageOperations.syntaxHighlight(
this.editor.getText(),
Model.getLanguage()));
this.refreshHighlighting();
}
/**
* Refreshes the editor whenever the language is changed.
* @param event Which language was switched to
*/
@Subscribe
private void handle(LanguageChangedEvent event) {
this.refreshHighlighting();
}
}