Skip to content

Commit

Permalink
Add syntax highlighting for list start characters #508
Browse files Browse the repository at this point in the history
  • Loading branch information
de-jcup committed Jan 13, 2025
1 parent 826a482 commit 4548145
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceVie
addPresentation(reconciler, ASCIIDOCTOR_COMMAND.getId(), getPreferences().getColor(COLOR_ASCIIDOCTOR_COMMAND), SWT.NONE);
addPresentation(reconciler, HEADLINE.getId(), getPreferences().getColor(COLOR_ASCIIDOCTOR_HEADLINES), SWT.BOLD);
addPresentation(reconciler, BLOCK_TITLE.getId(), getPreferences().getColor(COLOR_ASCIIDOCTOR_HEADLINES), SWT.ITALIC);
addPresentation(reconciler, LIST_CHARACTER.getId(), getPreferences().getColor(COLOR_LIST_CHARACTERS), SWT.BOLD);
addPresentation(reconciler, DELIMITERS.getId(), getPreferences().getColor(COLOR_DELIMITERS), SWT.NONE);
if (jfaceHyperlinkColor != null) {
addPresentation(reconciler, INCLUDE_KEYWORD.getId(), jfaceHyperlinkColor, SWT.BOLD);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2018 Albert Tregnaghi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions
* and limitations under the License.
*
*/
package de.jcup.asciidoctoreditor.document;

import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.Token;

public class AsciiDoctorCharacterPinPointRule implements IPredicateRule {

private IToken token;
private char[] start;
private char end;

public AsciiDoctorCharacterPinPointRule(char start, IToken token) {
this(new char[] { start }, ' ', token);
}

public AsciiDoctorCharacterPinPointRule(char[] start, IToken token) {
this(start, ' ', token);
}

public AsciiDoctorCharacterPinPointRule(char[] start, char end, IToken token) {
this.start = start;
this.end = end;
this.token = token;
}

@Override
public IToken evaluate(ICharacterScanner scanner) {
return evaluate(scanner, false);
}

@Override
public IToken evaluate(ICharacterScanner scanner, boolean resume) {

boolean startOfDocument = scanner.getColumn() == 0;
boolean newLine = startOfDocument;
if (!startOfDocument) {
scanner.unread();
int cbefore = scanner.read();
newLine = newLine || cbefore == '\n';
newLine = newLine || cbefore == '\r';
}

if (!newLine) {
return Token.UNDEFINED;
}
int c = -1;
Counter counter = new Counter();
for (int pos = 0; pos < start.length; pos++) {
counter.count++;
c=scanner.read();
if (c != start[pos]) {
counter.cleanup(scanner);
return Token.UNDEFINED;
}

}
int after = scanner.read();
counter.count++;
if (after == end) {
return token;
}
counter.cleanup(scanner);
return Token.UNDEFINED;
}

@Override
public IToken getSuccessToken() {
return token;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public enum AsciiDoctorDocumentIdentifiers implements AsciiDoctorDocumentIdentif
HEADLINE,

BLOCK_TITLE,

LIST_CHARACTER,

DELIMITERS,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public AsciiDoctorDocumentPartitionScanner() {
IToken includeKeyword = createToken(INCLUDE_KEYWORD);
IToken asciidoctorCommand = createToken(ASCIIDOCTOR_COMMAND);
IToken headline = createToken(HEADLINE);
IToken title = createToken(TITLE);
IToken blockTitle = createToken(BLOCK_TITLE);
IToken listEntry = createToken(LIST_CHARACTER);
IToken delimiters = createToken(DELIMITERS);

List<IPredicateRule> rules = new ArrayList<>();
Expand Down Expand Up @@ -81,6 +82,11 @@ public AsciiDoctorDocumentPartitionScanner() {
rules.add(new AsciiDoctorFormattedTextRule("*", "*", boldText));
rules.add(new AsciiDoctorFormattedTextRule("_", "_", italicText));

rules.add(new AsciiDoctorCharacterPinPointRule('-', listEntry));
rules.add(new AsciiDoctorCharacterPinPointRule('.', listEntry));
rules.add(new AsciiDoctorCharacterPinPointRule('*', listEntry));
rules.add(new AsciiDoctorCharacterPinPointRule("**".toCharArray(), listEntry) );
rules.add(new AsciiDoctorCharacterPinPointRule("***".toCharArray(), listEntry) );

rules.add(new SingleLineRule("<<", ">>", hyperlink, (char) -1, true));
rules.add(new SingleLineRule("xref:", "]", hyperlink, (char) -1, true));
Expand All @@ -91,12 +97,13 @@ public AsciiDoctorDocumentPartitionScanner() {
rules.add(new AsciiDoctorLineStartsWithRule(":", ":", false, knownVariables));
rules.add(new SingleLineRule("{", "}", knownVariables, (char) -1, true));

// https://docs.asciidoctor.org/asciidoc/latest/blocks/delimited/
rules.add(new AsciiDoctorLineContainsOnlyRule("....", delimiters)); // delimiter, delimited block
rules.add(new AsciiDoctorLineContainsOnlyRule("====", delimiters)); // delimiter admonition block syntax
rules.add(new AsciiDoctorLineContainsOnlyRule("****", delimiters)); // delimiter,delimited example

// block titles: see https://docs.asciidoctor.org/asciidoc/latest/blocks/add-title/
AsciiDoctorLineStartsWithRule blockTitleRule = new AsciiDoctorLineStartsWithRule(".", title);
AsciiDoctorLineStartsWithRule blockTitleRule = new AsciiDoctorLineStartsWithRule(".", blockTitle);
blockTitleRule.setSpaceAfterStartForbidden(true); // we do not want ". list item" but only ".my-title"
rules.add(blockTitleRule); // title

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public void initializeDefaultPreferences() {
preferences.setDefaultColor(COLOR_KNOWN_VARIABLES, DARK_GRAY);
preferences.setDefaultColor(COLOR_TEXT_BOLD, BLACK);
preferences.setDefaultColor(COLOR_TEXT_ITALIC, BLACK);
preferences.setDefaultColor(COLOR_LIST_CHARACTERS, DARK_GRAY);

preferences.setDefaultColor(COLOR_DELIMITERS, LIGHT_THEME_LIGHT_BLUE);

Expand All @@ -126,6 +127,7 @@ public void initializeDefaultPreferences() {
preferences.setDefaultColor(COLOR_PLANTUML_TYPE, KEYWORD_DEFAULT_PURPLE);
preferences.setDefaultColor(COLOR_PLANTUML_COLOR, DARK_BLUE);
preferences.setDefaultColor(COLOR_PLANTUML_DOUBLESTRING, ROYALBLUE);


/* ++++++++++++++ */
/* + Validation + */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,25 @@
* Constant definitions for plug-in preferences
*/
public enum AsciiDoctorEditorSyntaxColorPreferenceConstants implements PreferenceIdentifiable, PreferenceLabeled {
COLOR_ASCIIDOCTOR_HEADLINES("colorHeadlines", "Headlines"), COLOR_NORMAL_TEXT("colorNormalText", "Normal text"), COLOR_TEXT_BLOCKS("colorTextBlocks", "Text blocks"),
COLOR_TEXT_BOLD("colorTextBold", "Bold text"), COLOR_TEXT_ITALIC("colorTextItalic", "Italic"), COLOR_COMMENT("colorComments", "Comments"),
// COLOR_INCLUDE_KEYWORD("colorIncludeKeywords","Includes"),
COLOR_ASCIIDOCTOR_COMMAND("colorCommands", "Special AsciiDoctor commands"), COLOR_KNOWN_VARIABLES("colorKnownVariables", "Known variables"),
COLOR_ASCIIDOCTOR_HEADLINES("colorHeadlines", "Headlines"),

COLOR_NORMAL_TEXT("colorNormalText", "Normal text"),

COLOR_TEXT_BLOCKS("colorTextBlocks", "Text blocks"),

COLOR_TEXT_BOLD("colorTextBold", "Bold text"),

COLOR_TEXT_ITALIC("colorTextItalic", "Italic"),

COLOR_COMMENT("colorComments", "Comments"),

// COLOR_INCLUDE_KEYWORD("colorIncludeKeywords","Includes"),

COLOR_ASCIIDOCTOR_COMMAND("colorCommands", "Special AsciiDoctor commands"),

COLOR_KNOWN_VARIABLES("colorKnownVariables", "Known variables"),

COLOR_LIST_CHARACTERS("colorListCharacters", "List characters"),

COLOR_DELIMITERS("colorDelimiters", "Delimiters"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public void widgetSelected(SelectionEvent e) {
changeColor(editorMap, COLOR_ASCIIDOCTOR_COMMAND, AsciiDoctorEditorColorConstants.DARKTHEME_DEFAULT_COMMANDS);
changeColor(editorMap, COLOR_KNOWN_VARIABLES, AsciiDoctorEditorColorConstants.DARKTHEME_DEFAULT_KNOWN_VARIABLES);
changeColor(editorMap, COLOR_DELIMITERS, AsciiDoctorEditorColorConstants.LIGHT_THEME_LIGHT_BLUE);

changeColor(editorMap, COLOR_LIST_CHARACTERS, AsciiDoctorEditorColorConstants.DARK_THEME_LIGHT_BLUE);
}

private void changeColor(Map<AsciiDoctorEditorSyntaxColorPreferenceConstants, ColorFieldEditor> editorMap, AsciiDoctorEditorSyntaxColorPreferenceConstants colorId, RGB rgb) {
Expand Down

0 comments on commit 4548145

Please sign in to comment.