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

View File

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

View File

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