Add comment toggle feature

master
Oystein Kristoffer Tveit 2021-02-23 16:44:26 +01:00
parent 0ba350395b
commit 302b10a5a6
9 changed files with 170 additions and 5 deletions

View File

@ -19,7 +19,7 @@ Probably tastes better than any Apple editor and NetBeans combined.
- [X] Line numbers
- [X] Shortcuts
- [X] Change languages
- [ ] Toggle line comment
- [X] Toggle line comment
- [ ] Soft wrap
- [ ] Darkmode/Lightmode or color themes
- [ ] Add icons for files/folders in filetree

View File

@ -15,6 +15,7 @@ import org.fxmisc.richtext.model.TwoDimensional.Position;
import app.events.EditorChangedEvent;
import app.events.LanguageChangedEvent;
import app.events.ToggleCommentEvent;
import app.events.FileSaveStateChangedEvent;
import app.model.Model;
import app.service.LanguageOperations;
@ -63,6 +64,41 @@ public class EditorController implements Initializable, Controller {
this.setHighlighting(LanguageOperations.syntaxHighlight(this.editor.getText(), Model.getLanguage()));
}
/**
* Uses Model.language to determine whether the current line/selection
* is commented or not, and toggles the comment.
*/
private void toggleComment() {
if (editor.getSelectedText().equals("")) {
String currentLine = editor.getText(editor.getCurrentParagraph());
String newText;
if (Model.getLanguage().isCommentedLine(currentLine))
newText = Model.getLanguage().unCommentLine(currentLine);
else
newText = Model.getLanguage().commentLine(currentLine);
editor.replaceText(
editor.getCurrentParagraph(),
0,
editor.getCurrentParagraph(),
currentLine.length(),
newText
);
} else { // Comment selection
String newText;
if (Model.getLanguage().isCommentedSelection(editor.getSelectedText()))
newText = Model.getLanguage().unCommentSelection(editor.getSelectedText());
else
newText = Model.getLanguage().commentSelection(editor.getSelectedText());
editor.replaceSelection(newText);
}
}
/**
* Handles the event whenever the content of the editor is changed.
*/
@ -71,7 +107,6 @@ public class EditorController implements Initializable, Controller {
Position pos = this.editor.offsetToPosition(offset, Bias.Forward);
this.eventBus.post(new EditorChangedEvent(pos.getMajor() + 1, pos.getMinor()));
if (Model.getFileIsSaved())
this.eventBus.post(new FileSaveStateChangedEvent(false));
@ -80,11 +115,20 @@ public class EditorController implements Initializable, Controller {
/**
* Refreshes the editor whenever the language is changed.
* @param event Which language was switched to
* @param event
*/
@Subscribe
private void handle(LanguageChangedEvent event) {
this.refreshHighlighting();
}
/**
* Toggles a language specific comment of the line/selection
* @param event
*/
@Subscribe
private void handle(ToggleCommentEvent event) {
this.toggleComment();
}
}

View File

@ -9,6 +9,7 @@ import com.google.common.eventbus.Subscribe;
import app.events.ExitApplicationEvent;
import app.events.LanguageChangedEvent;
import app.events.ToggleCommentEvent;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
@ -82,6 +83,10 @@ public class MenubarController implements Initializable, Controller {
this.eventBus.register(this);
}
/* ------------------------------------------------------------------------ */
/* FILE */
/* ------------------------------------------------------------------------ */
/**
* Handles the event where the language was change from the menu.
* @param event
@ -102,6 +107,15 @@ public class MenubarController implements Initializable, Controller {
this.eventBus.post(new ExitApplicationEvent());
}
/* ------------------------------------------------------------------------ */
/* EDIT */
/* ------------------------------------------------------------------------ */
@FXML
private void handleToggleComment(ActionEvent event) {
this.eventBus.post(new ToggleCommentEvent());
}
/**
* Updates menubuttons whenever the language is changed
* @param event
@ -117,4 +131,9 @@ public class MenubarController implements Initializable, Controller {
.orElseThrow()
.setSelected(true);
}
/* ------------------------------------------------------------------------ */
/* ABOUT */
/* ------------------------------------------------------------------------ */
}

View File

@ -0,0 +1,5 @@
package app.events;
public class ToggleCommentEvent {
}

View File

@ -2,6 +2,7 @@ package app.languages;
import java.net.URL;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
@ -69,4 +70,45 @@ public class Java implements ProgrammingLanguage {
.map(e -> String.format("(?<%s>%s)", e.getValue(), e.getKey()))
.collect(Collectors.joining("|")));
}
// ----------------------------------------------------------------
public String commentLine(String line) {
return "// " + line;
}
public String unCommentLine(String line) {
return line.replaceFirst("// ?", "");
}
public boolean isCommentedLine(String line) {
return line.matches("// ?.*");
}
public String commentSelection(String selection) {
return
"/* \n" +
selection.lines()
.map(l -> " * " + l)
.collect(Collectors.joining("\n"))
+ "\n */";
}
public String unCommentSelection(String selection) {
String[] rawLines = selection.split("\n");
String[] lines = Arrays.copyOfRange(rawLines, 1, rawLines.length - 1);
return Arrays
.stream(lines)
.map(l -> l.replaceFirst("^ \\* ", ""))
.collect(Collectors.joining("\n"));
}
public boolean isCommentedSelection(String selection) {
// TODO: Fix
return selection.startsWith("/*");
}
}

View File

@ -2,6 +2,7 @@ package app.languages;
import java.net.URL;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
@ -57,4 +58,48 @@ public class Markdown implements ProgrammingLanguage {
.collect(Collectors.joining("|")));
}
// ----------------------------------------------------------------
// TODO: fix spacing of arrows
public String commentLine(String line) {
return "<!-- " + line + " -->";
}
public String unCommentLine(String line) {
return line
.replaceFirst("<!-- ?", "")
.replaceAll("-->\\s+?", "");
}
public boolean isCommentedLine(String line) {
return line.startsWith("<!--") && line.endsWith("-->");
}
public String commentSelection(String selection) {
return
"<!-- " +
selection.lines()
.map(l -> " * " + l)
.collect(Collectors.joining("\n"))
+ " -->";
}
public String unCommentSelection(String selection) {
var lines = selection.split("\n");
var firstLine = lines[0];
var lastLine = lines[lines.length - 1];
var midlines = Arrays.copyOfRange(lines, 1, lines.length - 1);
return
firstLine.replaceFirst("<!-- ", "")
+ String.join("\n", midlines)
+ lastLine.replaceAll(" -->\\s+?", "");
}
public boolean isCommentedSelection(String selection) {
return selection.startsWith("<!--") && selection.endsWith("-->");
}
}

View File

@ -25,4 +25,13 @@ public interface ProgrammingLanguage {
* The pattern containing all regexes for syntax highlighting
*/
public Pattern getPattern();
public String commentLine(String line);
public String unCommentLine(String line);
public boolean isCommentedLine(String line);
public String commentSelection(String selection);
public String unCommentSelection(String selection);
public boolean isCommentedSelection(String selection);
}

View File

@ -45,7 +45,7 @@
</items>
</Menu>
<SeparatorMenuItem/>
<MenuItem mnemonicParsing="false" text="Exit" accelerator="Shortcut+w"/>
<MenuItem mnemonicParsing="false" text="Exit" accelerator="Shortcut+w" onAction="#handleExitApplication"/>
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
@ -60,7 +60,7 @@
<MenuItem mnemonicParsing="false" text="Find" accelerator="Shortcut+f"/>
<MenuItem mnemonicParsing="false" text="Replace" accelerator="Shortcut+h"/>
<SeparatorMenuItem/>
<MenuItem mnemonicParsing="false" text="Toggle line comment" accelerator="Shortcut+k"/>
<MenuItem mnemonicParsing="false" text="Toggle line comment" accelerator="Shortcut+k" onAction="#handleToggleComment"/>
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">

View File

@ -12,6 +12,7 @@
alignment="CENTER_LEFT">
<Label text="Modeline :)"/>
<Region HBox.hgrow="ALWAYS"/>
<Label fx:id="saveState" text="Saved!"/>
<Label fx:id="columnrow" text="[y:x]"/>
<Label fx:id="language" text="Java"/>