Commit 685a469c authored by Alexander Weigl's avatar Alexander Weigl

fixes in completionposition

parent 3e0f3a67
......@@ -15,16 +15,17 @@ public class CompletionPosition {
int pos;
public String getPrefix() {
int start;
for (start = pos; start >= 0; start--) {
if (text.isEmpty()) return "";
int start = Math.min(text.length() - 1, pos - 1);
for (; start >= 0; start--) {
if (Character.isWhitespace(text.charAt(start)))
break;
}
return text.substring(start, pos).trim();
return text.substring(Math.max(0, start), pos).trim();
}
public boolean onLineBegin() {
for (int i = pos; i >= 0; i--) {
for (int i = Math.min(text.length() - 1, pos); i >= 0; i--) {
if (text.charAt(i) == ' ' || text.charAt(i) == '\t') continue;
if (text.charAt(i) == '\n') return true;
return false;
......
......@@ -33,10 +33,7 @@ import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
......@@ -62,6 +59,9 @@ import org.fxmisc.richtext.*;
import org.fxmisc.richtext.event.MouseOverTextEvent;
import org.fxmisc.richtext.model.*;
import org.fxmisc.undo.UndoManager;
import org.fxmisc.wellbehaved.event.EventPattern;
import org.fxmisc.wellbehaved.event.InputMap;
import org.fxmisc.wellbehaved.event.Nodes;
import org.reactfx.EventStream;
import org.reactfx.SuspendableNo;
import org.reactfx.collection.LiveList;
......@@ -78,6 +78,11 @@ import java.util.function.IntFunction;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;
import static javafx.scene.input.KeyCombination.SHORTCUT_DOWN;
import static org.fxmisc.wellbehaved.event.EventPattern.keyPressed;
import static org.fxmisc.wellbehaved.event.InputHandler.Result.PROCEED;
import static org.fxmisc.wellbehaved.event.InputMap.*;
/**
* ScriptArea is the {@link CodeArea} for writing Proof Scripts.
* <p>
......@@ -145,25 +150,43 @@ public class ScriptArea extends BorderPane {
private void init() {
codeArea.setAutoScrollOnDragDesired(false);
codeArea.setOnKeyReleased(event -> {
InputMap<KeyEvent> inputMap = sequence(
process(EventPattern.keyPressed(),
(e) -> {
if (autoCompletion.isVisible()) autoCompletion.update();
inlineToolbar.hide();
if (event.isControlDown() && event.getCode() == KeyCode.ENTER)
simpleReformat();
if (event.isControlDown() && event.getCode() == KeyCode.H)
inlineToolbar.show();
if (event.isControlDown() && event.getCode() == KeyCode.SPACE) {
return PROCEED;
}),
consumeWhen(keyPressed(KeyCode.ENTER), autoCompletion::isVisible,
e -> autoCompletion.complete()),
consume(keyPressed(KeyCode.ENTER, SHORTCUT_DOWN),
(e) -> simpleReformat()),
consume(keyPressed(KeyCode.H, SHORTCUT_DOWN),
(e) -> inlineToolbar.show()),
consume(keyPressed(KeyCode.SPACE, SHORTCUT_DOWN), e -> {
if (autoCompletion.isVisible()) {
autoCompletion.hide();
} else {
autoCompletion.update();
autoCompletion.show();
}
if (autoCompletion.isVisible())
autoCompletion.update();
});
}),
consumeWhen(keyPressed(KeyCode.DOWN), autoCompletion::isVisible, e -> autoCompletion.selection(+1)),
consumeWhen(keyPressed(KeyCode.UP), autoCompletion::isVisible, e -> autoCompletion.selection(-1)),
consumeWhen(keyPressed(KeyCode.PAGE_DOWN), autoCompletion::isVisible, e -> autoCompletion.selection(+3)),
consumeWhen(keyPressed(KeyCode.PAGE_UP), autoCompletion::isVisible, e -> autoCompletion.selection(-3)),
consume(keyPressed(KeyCode.ESCAPE), e -> {
autoCompletion.hide();
inlineToolbar.hide();
}));
Nodes.addInputMap(codeArea, inputMap);
setCenter(scrollPane);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
scrollPane.widthProperty().addListener((a, b, c) -> {
codeArea.setMinSize(scrollPane.getWidth(), scrollPane.getHeight());
});
scrollPane.widthProperty().addListener((a, b, c) ->
codeArea.setMinSize(scrollPane.getWidth(), scrollPane.getHeight()));
/*
scrollPane.estimatedScrollYProperty().addListener(new ChangeListener<Double>() {
......@@ -551,7 +574,6 @@ public class ScriptArea extends BorderPane {
public CodePointCharStream getStream() {
return CharStreams.fromString(getText(), getFilePath().getAbsolutePath());
}
public String getText() {
......@@ -1801,6 +1823,7 @@ public class ScriptArea extends BorderPane {
/*private AutoCompletePopup<Suggestion> popup = new AutoCompletePopup<>();
private ListView<Suggestion> suggestionView;
private ObservableList<Suggestion> suggestions;*/
private int lastSelected = -1;
private Stage popup;
private ListView<Suggestion> suggestionView = new ListView<>();
private ObservableList<Suggestion> suggestions;
......@@ -1811,7 +1834,11 @@ public class ScriptArea extends BorderPane {
popup.setSkin(new AutoCompletePopupSkin<>(popup));
suggestionView = (ListView<Suggestion>) popup.getSkin().getNode();*/
suggestions = suggestionView.getItems();
suggestionView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
suggestionView.getSelectionModel().getSelectedIndices().addListener((InvalidationListener) observable -> {
System.out.println(" = " + suggestionView.getSelectionModel().getSelectedIndex());
//lastSelected = suggestionView.getSelectionModel().getSelectedIndex();
});
//popup.setVisibleRowCount(5);
suggestionView.setEditable(false);
......@@ -1844,6 +1871,7 @@ public class ScriptArea extends BorderPane {
//System.out.println("searchPrefix = " + searchPrefix);
CompletionPosition cp = new CompletionPosition(getText(), end);
System.out.println("cp.getPrefix() = " + cp.getPrefix());
List<Suggestion> newS = autoCompletionController.getSuggestions(cp);
suggestions.setAll(newS);
......@@ -1857,7 +1885,7 @@ public class ScriptArea extends BorderPane {
public void show() {
//popup.show(ScriptArea.this.getScene().getWindow());
popup.show();
codeArea.requestFocus();
}
public void hide() {
......@@ -1867,6 +1895,28 @@ public class ScriptArea extends BorderPane {
public boolean isVisible() {
return popup != null && popup.isShowing();
}
public void complete() {
String entry = suggestions.get(lastSelected).getText();
codeArea.selectWord();
codeArea.replaceSelection(entry);
hide();
codeArea.requestFocus();
}
public void selection(int relline) {
if (lastSelected < 0) {
if (relline < 0) {
lastSelected = suggestionView.getItems().size() - 1;
} else {
lastSelected = 0;
}
} else {
lastSelected += relline;
}
suggestionView.getSelectionModel().select(lastSelected);
suggestionView.scrollTo(lastSelected);
}
}
}
......
package edu.kit.iti.formal.psdbg.gui.actions.acomplete;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* @author Alexander Weigl
* @version 1 (14.03.18)
*/
public class CompletionPositionTest {
private CompletionPosition a, b, c, d, e;
@Before
public void setup() {
a = create("abc| def ghi");
b = create("abc |def ghi");
c = create("abc\ndef\nghi\n|");
d = new CompletionPosition("", 3);
e = create("abc\ndef\n|\nghi\n");
}
private static CompletionPosition create(String s) {
return new CompletionPosition(s.replace("|", ""), s.indexOf('|'));
}
@Test
public void getPrefix() throws Exception {
assertEquals("abc", a.getPrefix());
assertEquals("", b.getPrefix());
assertEquals("", c.getPrefix());
assertEquals("", d.getPrefix());
assertEquals("", e.getPrefix());
}
@Test
public void onLineBegin() throws Exception {
assertFalse(a.onLineBegin());
assertFalse(b.onLineBegin());
assertTrue(c.onLineBegin());
assertTrue(d.onLineBegin());
assertTrue(e.onLineBegin());
}
@Test
public void filterByPrefix() throws Exception {
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment