Commit 22dbba6a authored by Miklos Magyari's avatar Miklos Magyari
Browse files

Implemented semantic highlighting for code peek


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent 6ab70e21
......@@ -217,6 +217,17 @@ public final class AstSyntaxHighlightTokens {
}
return syntaxMap.get(offset);
}
public static IToken getSyntaxToken(Location location) {
if (location == null) {
return null;
}
final IFile file = (IFile)location.getFile();
if (file != null) {
return getSyntaxToken(file.getFullPath().toOSString(), location.getOffset());
}
return null;
}
public static synchronized void update(String filename, DocumentEvent event) {
final IntObjectHashMap<IToken> syntaxMap = AstSyntaxHighlightTokens.getOffsetMap(filename);
......
......@@ -30,11 +30,13 @@ import org.eclipse.ui.texteditor.ITextEditor;
* @author Adam Knapp
* */
public class PeekSource {
private static Location lastPeekedLocation;
public static String getPeekSource(IEditorPart targetEditor, Location codeLoc) {
if (codeLoc == null || codeLoc.equals(NULL_Location.INSTANCE)) {
return null;
}
final IResource fileToLoad = codeLoc.getFile();
IResource actual = (IResource)targetEditor.getEditorInput().getAdapter(IFile.class);
......@@ -69,7 +71,8 @@ public class PeekSource {
return null;
}
}
lastPeekedLocation = codeLoc;
return codeText;
}
return null;
......@@ -124,7 +127,7 @@ public class PeekSource {
}
IDocument doc = new Document(source);
CodeScanner scanner = new CodeScanner(new ColorManager(), true);
CodeScanner scanner = new CodeScanner(new ColorManager(), lastPeekedLocation);
scanner.setRange(doc, 0, source.length());
IToken token;
StringBuilder codestr = new StringBuilder("<pre>");
......
......@@ -15,6 +15,7 @@ import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WordRule;
import org.eclipse.titan.designer.Activator;
import org.eclipse.titan.designer.AST.Location;
import org.eclipse.titan.designer.AST.Module;
import org.eclipse.titan.designer.editors.AstSyntaxHighlightTokens;
import org.eclipse.titan.designer.editors.DocumentTracker;
......@@ -92,13 +93,21 @@ public class AstWordRule extends WordRule {
(isBracketColoringEnabled && isBracket)) {
final IDocumentGetter codeScanner = (IDocumentGetter)scanner;
final Module module = DocumentTracker.get(codeScanner.getDocument());
IToken decoration = null;
if (module != null) {
final String filename = module.getLocation().getFile().getFullPath().toOSString();
final IToken decoration = AstSyntaxHighlightTokens.getSyntaxToken(filename, codeScanner.getTokenOffset());
if (decoration != null) {
return decoration;
decoration = AstSyntaxHighlightTokens.getSyntaxToken(filename, codeScanner.getTokenOffset());
} else if (scanner instanceof CodeScanner) {
final Location peekLocation = ((CodeScanner)scanner).getPeekLocation();
if (peekLocation != null) {
final Location calcLocation = new Location(peekLocation);
calcLocation.setOffset(calcLocation.getOffset() + ((CodeScanner)scanner).getTokenOffset());
decoration = AstSyntaxHighlightTokens.getSyntaxToken(calcLocation);
}
}
if (decoration != null) {
return decoration;
}
}
IToken token= fWords.get(buffer);
......
......@@ -22,6 +22,7 @@ import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.WhitespaceRule;
import org.eclipse.jface.text.rules.WordRule;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.titan.designer.AST.Location;
import org.eclipse.titan.designer.editors.ColorManager;
import org.eclipse.titan.designer.editors.IDocumentGetter;
import org.eclipse.titan.designer.editors.NumberDetectionPatternRule;
......@@ -37,6 +38,8 @@ import org.eclipse.titan.designer.productUtilities.ProductConstants;
* @author Adam Knapp
*/
public final class CodeScanner extends RuleBasedScanner implements IDocumentGetter {
private Location peekLocation;
public static final String[] KEYWORDS = new String[] { "activate", "address", "alive", "all", "alt", "altstep", "and", "and4b", "any",
"break", "case", "component", "const", "continue", "control", "deactivate", "display", "do", "else", "encode", "enumerated",
"except", "exception", "execute", "extends", "extension", "external", "for", "from", "function", "goto", "group", "if",
......@@ -109,11 +112,12 @@ public final class CodeScanner extends RuleBasedScanner implements IDocumentGett
public static final String[] VISIBILITY_MODIFIERS = new String[] { "public", "private", "friend" };
public CodeScanner(final ColorManager colorManager) {
this(colorManager, false);
this(colorManager, null);
}
public CodeScanner(final ColorManager colorManager, boolean skipSemanticHighlightingRule) {
final List<IRule> rules = getTTCNRules(colorManager, skipSemanticHighlightingRule);
public CodeScanner(final ColorManager colorManager, Location peekLocation) {
this.peekLocation = peekLocation;
final List<IRule> rules = getTTCNRules(colorManager, peekLocation);
// line marker (single line preprocessor directive)
final IToken preprocessor = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_PREPROCESSOR);
......@@ -122,10 +126,10 @@ public final class CodeScanner extends RuleBasedScanner implements IDocumentGett
}
public static List<IRule> getTTCNRules(final ColorManager colorManager) {
return getTTCNRules(colorManager, false);
return getTTCNRules(colorManager, null);
}
public static List<IRule> getTTCNRules(final ColorManager colorManager, boolean skipSemanticHighlightingRule) {
public static List<IRule> getTTCNRules(final ColorManager colorManager, Location peekLocation) {
final IToken keyword = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_TTCN3_KEYWORDS);
final IToken templateMatch = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_TEMPLATE_MATCH);
final IToken types = colorManager.createTokenFromPreference(PreferenceConstants.COLOR_TYPE);
......@@ -153,8 +157,7 @@ public final class CodeScanner extends RuleBasedScanner implements IDocumentGett
rules.add(new StringDetectionPatternRule("'", new char[][] { { '\'', 'B' }, { '\'', 'H' }, { '\'', 'O' } }, string));
rules.add(new NumberDetectionPatternRule(ninteger, nfloat));
final WordRule wordRule = skipSemanticHighlightingRule ?
new WordRule(new WordDetector(), other) : new AstWordRule(new WordDetector(), other);
final WordRule wordRule = new AstWordRule(new WordDetector(), other);
for (final String element : CodeScanner.KEYWORDS) {
wordRule.addWord(element, keyword);
}
......@@ -277,4 +280,8 @@ public final class CodeScanner extends RuleBasedScanner implements IDocumentGett
return ranges.toArray(new StyleRange[ranges.size()]);
}
public Location getPeekLocation() {
return peekLocation;
}
}
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