Fix syntax highlight completing before char added

This commit is contained in:
Oystein Kristoffer Tveit 2021-02-23 12:59:08 +01:00
parent 4e24363e63
commit d4aff39f55
3 changed files with 16 additions and 25 deletions

View File

@ -3,6 +3,7 @@ package app.controllers;
import java.net.URL;
import java.util.Collection;
import java.util.ResourceBundle;
import java.util.concurrent.CompletableFuture;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
@ -19,7 +20,6 @@ import app.model.Model;
import app.service.LanguageOperations;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.input.KeyEvent;
public class EditorController implements Initializable, Controller {
@ -27,11 +27,18 @@ public class EditorController implements Initializable, Controller {
private CodeArea editor;
private EventBus eventBus;
private Model model;
/**
* Initializes the controller, and binds the event of change in editor content
* to {@link #editorChanged() editorChanged}
*/
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
editor.setParagraphGraphicFactory(LineNumberFactory.get(editor));
editor
.textProperty()
.addListener((obs, oldV, newV) -> this.editorChanged());
}
@Override
@ -42,6 +49,7 @@ public class EditorController implements Initializable, Controller {
/**
* Applies highlighting to the editor.
*
* @param highlighting highlighting data
*/
private void setHighlighting(StyleSpans<Collection<String>> highlighting) {
@ -52,26 +60,17 @@ public class EditorController implements Initializable, Controller {
* Recalculates and refreshes the syntax highlighting of the editor.
*/
private void refreshHighlighting() {
this.setHighlighting(
LanguageOperations.syntaxHighlight(
this.editor.getText(),
Model.getLanguage()));
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) {
public void editorChanged() {
int offset = this.editor.getCaretPosition();
Position pos = this.editor.offsetToPosition(offset, Bias.Forward);
this.eventBus.post(
new EditorChangedEvent(
event.getCode(),
pos.getMajor() + 1,
pos.getMinor()
));
this.eventBus.post(new EditorChangedEvent(pos.getMajor() + 1, pos.getMinor()));
this.refreshHighlighting();
}

View File

@ -1,23 +1,15 @@
package app.events;
import javafx.scene.input.KeyCode;
public class EditorChangedEvent extends Event {
private KeyCode keyCode;
private int lineNumber;
private int column;
public EditorChangedEvent(KeyCode keyCode, int lineNumber, int column) {
this.keyCode = keyCode;
public EditorChangedEvent(int lineNumber, int column) {
this.lineNumber = lineNumber;
this.column = column;
}
public KeyCode getKeyCode() {
return this.keyCode;
}
public int getLineNumber() {
return this.lineNumber;
}

View File

@ -9,6 +9,6 @@
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="app.controllers.EditorController">
<content>
<CodeArea fx:id="editor" onKeyPressed="#editorChanged"/>
<CodeArea fx:id="editor"/>
</content>
</VirtualizedScrollPane>