Skip to content

Commit

Permalink
Merge pull request #24 from BalduinLandolt/wip/code-completion
Browse files Browse the repository at this point in the history
Wip/code completion
  • Loading branch information
BalduinLandolt authored Nov 24, 2019
2 parents ba69162 + 9782f69 commit 251c119
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 48 deletions.
65 changes: 19 additions & 46 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>turboTranscriber</groupId>
<artifactId>turboTranscriber</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.4</version>
<packaging>jar</packaging>
<!-- <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -36,58 +36,31 @@
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.7.RELEASE</version>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>repackage</goal>
<goal>single</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<executable>true</executable>
<mainClass>
ch.blandolt.turboTranscriber.TurboTranscriberMain
</mainClass>
</configuration>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>ch.blandolt.turboTranscriber.TurboTranscriberMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

<!-- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
ch.blandolt.turboTranscriber.TurboTranscriberMain
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>-->

</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.jdom</groupId>
Expand All @@ -100,9 +73,9 @@
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.7.RELEASE</version>
<groupId>com.fifesoft</groupId>
<artifactId>autocomplete</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ private void refreshGUI() {
gui.createThumbnails();
gui.refreshEnabledComponents();

gui.refreshCodeCompletion();

// TODO: more?
}

Expand Down
48 changes: 46 additions & 2 deletions src/main/java/ch/blandolt/turboTranscriber/gui/MainGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
import ch.blandolt.turboTranscriber.util.Loggable;
import ch.blandolt.turboTranscriber.util.Settings;
import ch.blandolt.turboTranscriber.util.rsyntax.RawTokenMaker;
import ch.blandolt.turboTranscriber.util.rsyntax.TTCompletionProvider;
import ch.blandolt.turboTranscriber.util.rsyntax.WeightedCompletion;
import org.fife.ui.autocomplete.AutoCompletion;
import org.fife.ui.autocomplete.BasicCompletion;
import org.fife.ui.autocomplete.DefaultCompletionProvider;
import org.fife.ui.rsyntaxtextarea.*;
import org.fife.ui.rtextarea.RTextScrollPane;

Expand All @@ -17,7 +22,7 @@
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.*;
import java.util.List;

/**
Expand Down Expand Up @@ -79,6 +84,8 @@ public class MainGUI extends JFrame implements Loggable, WindowListener, Docume
private BufferedImage loadedImage;
private float imageScaling = 0.4f;

private DefaultCompletionProvider provider = null;

/**
* Constructor of the GUI.
* <p>
Expand Down Expand Up @@ -192,6 +199,7 @@ private void setUpMainWindow() {
// TODO: ensure loading in jars (might not work, not sure)

String path = "theme_light.xml";
// TODO: make dark theme more contrastive
//String path = "theme_dark.xml";
Log.log(getClass());
InputStream in = getClass().getClassLoader().getResourceAsStream(path);
Expand All @@ -200,7 +208,6 @@ private void setUpMainWindow() {
} catch (IOException ioe) { // Never happens
ioe.printStackTrace();
}
//transcriptionSyntaxTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_C);
Font prev = transcriptionSyntaxTextArea.getFont();
transcriptionSyntaxTextArea.setBracketMatchingEnabled(true);
transcriptionSyntaxTextArea.setPaintMatchedBracketPair(true);
Expand All @@ -210,6 +217,8 @@ private void setUpMainWindow() {
transcriptionSyntaxTextArea.setFont(new Font(prev.getName(), prev.getStyle(), prev.getSize()+4));
// TODO make font size a setting

setUpCodeCompletion();

xmlScroller = new RTextScrollPane(xmlArea);
styledScroller = new JScrollPane(new JLabel("Imagine nice HTML here."));
splitterXMLStuff = new JSplitPane(JSplitPane.VERTICAL_SPLIT, xmlScroller, styledScroller);
Expand Down Expand Up @@ -238,6 +247,41 @@ private void setUpMainWindow() {
logPane.add(logScroller, BorderLayout.CENTER);
}

private void setUpCodeCompletion() {
provider = new TTCompletionProvider();
provider.setAutoActivationRules(true, "({[;");
AutoCompletion ac = new AutoCompletion(provider);
ac.setAutoActivationEnabled(true);
ac.install(transcriptionSyntaxTextArea);
}

public void refreshCodeCompletion() {
List<String> tokens = getCompletionTokens();
HashMap<String, Integer> types = new HashMap<>();
for (String token: tokens){
if (token.equals(""))
continue;
if (types.containsKey(token)){
Integer v2 = types.get(token)+1;
types.put(token, v2);
} else {
types.put(token, Integer.valueOf(1));
}
}
provider.clear();
for (Map.Entry<String, Integer> entry: types.entrySet()){
// TODO: get weighted completion to work
//provider.addCompletion(new WeightedCompletion(provider, entry.getKey(), entry.getValue()));
provider.addCompletion(new BasicCompletion(provider, entry.getKey(), entry.getValue().toString()));
}
}

private List<String> getCompletionTokens() {
String s = transcriptionSyntaxTextArea.getText();
s = s.replaceAll("\n", " ");
return Arrays.asList(s.split(" "));
}

private void handle_listeners() {

// TODO: Add all necessary listeners
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ch.blandolt.turboTranscriber.util.rsyntax;

import org.fife.ui.autocomplete.DefaultCompletionProvider;

public class TTCompletionProvider extends DefaultCompletionProvider {

@Override
protected boolean isValidChar(char ch) {
return super.isValidChar(ch) || ch=='{' || ch== '(' || ch=='[' || ch==';';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ch.blandolt.turboTranscriber.util.rsyntax;

import org.fife.ui.autocomplete.BasicCompletion;
import org.fife.ui.autocomplete.Completion;
import org.fife.ui.autocomplete.CompletionProvider;

public class WeightedCompletion extends BasicCompletion {

private int weight = 1;

public WeightedCompletion(CompletionProvider provider, String replacementText) {
this(provider, replacementText, 1);
}

public WeightedCompletion(CompletionProvider provider, String replacementText, int weight) {
super(provider, replacementText, "("+weight+") ");
this.weight = weight;
}

public WeightedCompletion(CompletionProvider provider, String replacementText, String shortDesc) {
this(provider, replacementText, shortDesc, 1);
}

public WeightedCompletion(CompletionProvider provider, String replacementText, String shortDesc, int weight) {
super(provider, replacementText, "("+weight+") "+shortDesc);
this.weight = weight;
}

public WeightedCompletion(CompletionProvider provider, String replacementText, String shortDesc, String summary) {
this(provider, replacementText, shortDesc, summary, 1);
}

public WeightedCompletion(CompletionProvider provider, String replacementText, String shortDesc, String summary, int weight) {
super(provider, replacementText, "("+weight+") "+shortDesc, summary);
this.weight = weight;
}

public int getWeight() {
return weight;
}

@Override
public int compareTo(Completion other){
if (other instanceof WeightedCompletion){
WeightedCompletion wc = (WeightedCompletion)other;
return -1*Integer.valueOf(getWeight()).compareTo(Integer.valueOf(wc.getWeight()));
} else {
return super.compareTo(other);
}

}
}

0 comments on commit 251c119

Please sign in to comment.