Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ide/spellchecker.bindings.htmlxml/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,27 @@
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.openide.util.ui</code-name-base>
<code-name-base>org.openide.util</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>9.3</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.openide.util</code-name-base>
<code-name-base>org.openide.util.lookup</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>9.3</specification-version>
<specification-version>8.16</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.openide.util.lookup</code-name-base>
<code-name-base>org.openide.util.ui</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>8.16</specification-version>
<specification-version>9.3</specification-version>
</run-dependency>
</dependency>
</module-dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@
*
* @author Riha, Poppenreiter
*/
public abstract class AbstractTokenList implements TokenList {
public abstract sealed class AbstractTokenList implements TokenList permits HtmlTokenList, XmlTokenList {

protected BaseDocument doc;
record SpellSpan(int begin, int end) {
public static final SpellSpan NONE = new SpellSpan(-1, -1);
};

protected final BaseDocument doc;
private CharSequence currentWord;
private int currentStartOffset;
protected int nextSearchOffset;
Expand All @@ -49,6 +53,7 @@ public abstract class AbstractTokenList implements TokenList {
this.doc = doc;
}

@Override
public void setStartOffset(int offset) {
currentWord = null;
currentStartOffset = (-1);
Expand All @@ -61,37 +66,40 @@ public void setStartOffset(int offset) {
}
}

@Override
public int getCurrentWordStartOffset() {
return currentStartOffset;
}

@Override
public CharSequence getCurrentWordText() {
return currentWord;
}

protected abstract int[] findNextSpellSpan() throws BadLocationException;
abstract SpellSpan findNextSpellSpan() throws BadLocationException;

@Override
public boolean nextWord() {
boolean next = nextWordImpl();
while (next && (currentStartOffset + currentWord.length()) < ignoreBefore)

while (next && (currentStartOffset + currentWord.length()) < ignoreBefore) {
next = nextWordImpl();

}

return next;
}

private boolean nextWordImpl() {
try {
int[] span = findNextSpellSpan();
SpellSpan span = findNextSpellSpan();

while (span[0] != -1) {
int offset = (span[0] < nextSearchOffset) ? nextSearchOffset : span[0];
while (span.begin != -1) {
int offset = (span.begin < nextSearchOffset) ? nextSearchOffset : span.begin;

int length = span[1];
boolean searching = true;

/* find next word */
while (offset < length) {
while (offset < span.end) {
String t = doc.getText(offset, 1);
char c = t.charAt(0);

Expand Down Expand Up @@ -130,10 +138,12 @@ private boolean nextWordImpl() {
}
}

@Override
public void addChangeListener(ChangeListener l) {
//ignored...
}

@Override
public void removeChangeListener(ChangeListener l) {
//ignored...
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
*
* @author Tor Norbye, Marek Fukala
*/
public class HtmlTokenList extends AbstractTokenList {
public final class HtmlTokenList extends AbstractTokenList {

private String fileType;
private final String fileType;
private boolean hidden = false;
private Iterator<TokenSequence<?>> tss;
private TokenSequence<?> ts;
Expand Down Expand Up @@ -84,16 +84,16 @@ public void setStartOffset(int offset) {
} else {
//no html code in the file
}

}

//fast hack for making the spellchecking embedding aware, should be fixed properly
//performance: the current approach is wrong since a new token sequence is obtained
//and positioned for each search offset!
@Override
protected int[] findNextSpellSpan() throws BadLocationException {
protected SpellSpan findNextSpellSpan() throws BadLocationException {
if (ts == null || !ts.isValid() || hidden) {
return new int[]{-1, -1};
return SpellSpan.NONE;
}

ts.move(nextSearchOffset);
Expand All @@ -102,7 +102,7 @@ protected int[] findNextSpellSpan() throws BadLocationException {
TokenId id = ts.token().id();

if (id == HTMLTokenId.SGML_COMMENT || id == HTMLTokenId.BLOCK_COMMENT || id == HTMLTokenId.TEXT) {
return new int[]{ts.offset(), ts.offset() + ts.token().length()};
return new SpellSpan(ts.offset(), ts.offset() + ts.token().length());
}
}

Expand All @@ -112,7 +112,7 @@ protected int[] findNextSpellSpan() throws BadLocationException {
return findNextSpellSpan();
}

return new int[]{-1, -1};
return SpellSpan.NONE;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.netbeans.modules.spellchecker.bindings.htmlxml;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -37,36 +36,29 @@
*
* @author Poppenreiter, Riha
*/
public class HtmlXmlTokenListProvider implements TokenListProvider {
public final class HtmlXmlTokenListProvider implements TokenListProvider {

private static final Map<String, String> MIME_TO_SETTING_NAME = new HashMap<String, String>();
static {
MIME_TO_SETTING_NAME.put("text/html", "HTML"); //NOI18N
MIME_TO_SETTING_NAME.put("text/xhtml", "XHTML"); //NOI18N
MIME_TO_SETTING_NAME.put("text/x-jsp", "JSP"); //NOI18N
MIME_TO_SETTING_NAME.put("text/x-tag", "JSP"); //NOI18N
MIME_TO_SETTING_NAME.put("text/x-gsp", "GSP"); //NOI18N
MIME_TO_SETTING_NAME.put("text/x-php5", "PHP"); //NOI18N
}
private static final Map<String, String> MIME_TO_SETTING_NAME = Map.ofEntries(
Map.entry("text/html", "HTML"), //NOI18N
Map.entry("text/xhtml", "XHTML"), //NOI18N
Map.entry("text/x-jsp", "JSP"), //NOI18N
Map.entry("text/x-tag", "JSP"), //NOI18N
Map.entry("text/x-gsp", "GSP"), //NOI18N
Map.entry("text/x-php5", "PHP") //NOI18N
);

/** Creates a new instance of RubyTokenListProvider */
public HtmlXmlTokenListProvider() {
}

@Override
public TokenList findTokenList(Document doc) {
if (!(doc instanceof BaseDocument)) {
Logger.getLogger(HtmlXmlTokenListProvider.class.getName()).log(Level.INFO, null,
new IllegalStateException("The given document is not an instance of the BaseDocument, is just " + //NOI18N
doc.getClass().getName()));
return null;
}
if (doc instanceof BaseDocument bdoc) {
//html
String docMimetype = NbEditorUtilities.getMimeType(doc);

//html
final BaseDocument bdoc = (BaseDocument) doc;
final String docMimetype = NbEditorUtilities.getMimeType(doc);
final AtomicReference<TokenList> ret = new AtomicReference<TokenList>();
doc.render(new Runnable() {
public void run() {
AtomicReference<TokenList> ret = new AtomicReference<>();
bdoc.render(() -> {
TokenHierarchy<?> th = TokenHierarchy.get(bdoc);
Set<LanguagePath> paths = th.languagePaths();
for(LanguagePath path : paths) {
Expand All @@ -76,17 +68,22 @@ public void run() {
break;
}
}
});

if(ret.get() != null) {
return ret.get();
}
});
if(ret.get() != null) {
return ret.get();
}

//xml
if ((docMimetype != null) && (docMimetype.contains("xml"))) { //NOI18N
return new XmlTokenList(bdoc);
}
//xml
if ((docMimetype != null) && (docMimetype.contains("xml"))) { //NOI18N
return new XmlTokenList(bdoc);
}
} else {
Logger.getLogger(HtmlXmlTokenListProvider.class.getName()).log(Level.INFO, null,
new IllegalStateException("The given document is not an instance of the BaseDocument, is just " + //NOI18N
(doc != null ? doc.getClass().getName() : "<null>")));

}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*
* @author Tor Norbye
*/
public class XmlTokenList extends AbstractTokenList {
public final class XmlTokenList extends AbstractTokenList {


private boolean hidden = false;
Expand All @@ -51,11 +51,12 @@ public void setStartOffset(int offset) {
hidden = Boolean.TRUE.equals (b);
}

protected int[] findNextSpellSpan() throws BadLocationException {
@Override
protected SpellSpan findNextSpellSpan() throws BadLocationException {
TokenHierarchy<Document> h = TokenHierarchy.get((Document) doc);
TokenSequence<?> ts = h.tokenSequence();
if (ts == null || hidden) {
return new int[]{-1, -1};
return SpellSpan.NONE;
}

ts.move(nextSearchOffset);
Expand All @@ -64,9 +65,9 @@ protected int[] findNextSpellSpan() throws BadLocationException {
TokenId id = ts.token().id();

if (id == XMLTokenId.BLOCK_COMMENT || id == XMLTokenId.TEXT) {
return new int[]{ts.offset(), ts.offset() + ts.token().length()};
return new SpellSpan(ts.offset(), ts.offset() + ts.token().length());
}
}
return new int[]{-1, -1};
return SpellSpan.NONE;
}
}
Loading