1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 01:15:29 +02:00

Cleanups to asm editor to fix highlighting, follow color

preferences and add keyworks
This commit is contained in:
Sebastien Marineau 2002-09-05 15:03:44 +00:00
parent 9de625f710
commit 3b2d390a93
5 changed files with 186 additions and 72 deletions

View file

@ -9,6 +9,8 @@ package org.eclipse.cdt.internal.ui.editor.asm;
import org.eclipse.cdt.internal.ui.text.AbstractCScanner;
import org.eclipse.cdt.internal.ui.text.ICColorConstants;
import org.eclipse.cdt.internal.ui.text.IColorManager;
import org.eclipse.cdt.internal.ui.text.util.CWhitespaceDetector;
import java.util.ArrayList;
import java.util.List;
@ -20,6 +22,7 @@ import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.SingleLineRule;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WhitespaceRule;
import org.eclipse.jface.text.rules.WordPatternRule;
import org.eclipse.jface.text.rules.WordRule;
import org.eclipse.jface.util.PropertyChangeEvent;
@ -30,54 +33,14 @@ import org.eclipse.jface.util.PropertyChangeEvent;
*/
public final class AsmCodeScanner extends AbstractCScanner {
private static class VersionedWordRule extends WordRule {
private final String fVersion;
private final boolean fEnable;
private String fCurrentVersion;
public VersionedWordRule(IWordDetector detector, String version, boolean enable, String currentVersion) {
super(detector);
fVersion= version;
fEnable= enable;
fCurrentVersion= currentVersion;
}
public void setCurrentVersion(String version) {
fCurrentVersion= version;
}
/*
* @see IRule#evaluate
*/
public IToken evaluate(ICharacterScanner scanner) {
IToken token= super.evaluate(scanner);
if (fEnable) {
if (fCurrentVersion.equals(fVersion))
return token;
return Token.UNDEFINED;
} else {
if (fCurrentVersion.equals(fVersion))
return Token.UNDEFINED;
return token;
}
}
}
private static String[] fgKeywords= {
".set", ".section",
".global",
".global",".file",
".extern", ".macro", ".endm",
".if", ".ifdef", ".ifndef", ".else", ".endif",
".include", ".globl",
".text",".data", ".rodata", ".common", ".debug", ".ctor", ".dtor",
".asciz", ".byte", ".long", ".size", ".align", ".type"
".ascii", ".asciz", ".byte", ".long", ".size", ".align", ".type"
};
@ -91,8 +54,6 @@ public final class AsmCodeScanner extends AbstractCScanner {
ICColorConstants.C_DEFAULT
};
private VersionedWordRule fVersionedWordRule;
/**
* Creates a C code scanner
*/
@ -117,8 +78,6 @@ public final class AsmCodeScanner extends AbstractCScanner {
// Add rule for strings
Token token= getToken(ICColorConstants.C_SINGLE_LINE_COMMENT);
// Add rule for single line comments.
rules.add(new EndOfLineRule("//", token));
// Add rule for single line comments.
rules.add(new EndOfLineRule("#", token));
@ -126,12 +85,12 @@ public final class AsmCodeScanner extends AbstractCScanner {
token= getToken(ICColorConstants.C_STRING);
// Add rule for strings and character constants.
rules.add(new SingleLineRule("'", "'", token, '\\'));
rules.add(new SingleLineRule("\"", "\"", token, '\\'));
//rules.add(new SingleLineRule("\"", "\"", token, '\\'));
Token other= getToken(ICColorConstants.C_DEFAULT);
// Add generic whitespace rule.
//rules.add(new WhitespaceRule(new CWhitespaceDetector()));
rules.add(new WhitespaceRule(new CWhitespaceDetector()));
// Add word rule for labels
WordRule labelRule = new WordRule(new AsmWordDetector(false), other) {
@ -212,14 +171,6 @@ public final class AsmCodeScanner extends AbstractCScanner {
* @see RuleBasedScanner#setRules(IRule[])
*/
public void setRules(IRule[] rules) {
int i;
for (i= 0; i < rules.length; i++)
if (rules[i].equals(fVersionedWordRule))
break;
// not found - invalidate fVersionedWordRule
if (i == rules.length)
fVersionedWordRule= null;
super.setRules(rules);
}

View file

@ -8,6 +8,9 @@ package org.eclipse.cdt.internal.ui.editor.asm;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.cdt.internal.ui.text.CPartitionScanner;
import org.eclipse.cdt.internal.ui.text.ICColorConstants;
import org.eclipse.jface.text.rules.EndOfLineRule;
import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.IRule;
@ -16,6 +19,7 @@ import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.MultiLineRule;
import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.SingleLineRule;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WordRule;
@ -29,7 +33,9 @@ public class AsmPartitionScanner extends RuleBasedPartitionScanner {
private final static String SKIP= "__skip";
public final static String C_MULTILINE_COMMENT= "__c_multiline_comment";
public final static String ASM_MULTILINE_COMMENT= ICColorConstants.C_MULTI_LINE_COMMENT;
public final static String ASM_SINGLE_LINE_COMMENT= ICColorConstants.C_SINGLE_LINE_COMMENT;
public final static String ASM_STRING= ICColorConstants.C_STRING;
/**
@ -92,7 +98,11 @@ public class AsmPartitionScanner extends RuleBasedPartitionScanner {
public AsmPartitionScanner() {
super();
IToken comment= new Token(C_MULTILINE_COMMENT);
IToken comment= new Token(CPartitionScanner.C_MULTILINE_COMMENT);
IToken single_comment= new Token(CPartitionScanner.C_SINGLE_LINE_COMMENT);
IToken string= new Token(CPartitionScanner.C_STRING);
IToken skip= new Token(SKIP);
List rules= new ArrayList();
@ -103,12 +113,11 @@ public class AsmPartitionScanner extends RuleBasedPartitionScanner {
// Add rule for single line comments.
//rules.add(new EndOfLineRule("//", Token.UNDEFINED));
rules.add(new EndOfLineRule("//", single_comment));
// Add rule for strings and character constants.
//rules.add(new SingleLineRule("\"", "\"", Token.UNDEFINED, '\\'));
//rules.add(new SingleLineRule("'", "'", Token.UNDEFINED, '\\'));
rules.add(new SingleLineRule("\"", "\"", string, '\\'));
rules.add(new SingleLineRule("'", "'", string, '\\'));
EmptyCommentRule wordRule= new EmptyCommentRule(comment);
rules.add(wordRule);

View file

@ -13,6 +13,7 @@ import org.eclipse.jface.text.TextPresentation;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.custom.StyleRange;
@ -24,6 +25,33 @@ public class AsmSourceViewerConfiguration extends SourceViewerConfiguration {
private AsmTextEditor fEditor;
private AsmTextTools fTextTools;
/**
* Returns the ASM multiline comment scanner for this configuration.
*
* @return the ASM multiline comment scanner
*/
protected RuleBasedScanner getMultilineCommentScanner() {
return fTextTools.getMultilineCommentScanner();
}
/**
* Returns the ASM singleline comment scanner for this configuration.
*
* @return the ASM singleline comment scanner
*/
protected RuleBasedScanner getSinglelineCommentScanner() {
return fTextTools.getSinglelineCommentScanner();
}
/**
* Returns the ASM string scanner for this configuration.
*
* @return the ASM string scanner
*/
protected RuleBasedScanner getStringScanner() {
return fTextTools.getStringScanner();
}
/**
* Constructor for AsmSourceViewerConfiguration
*/
@ -51,15 +79,31 @@ public class AsmSourceViewerConfiguration extends SourceViewerConfiguration {
dr= new DefaultDamagerRepairer(fTextTools.getCodeScanner());
reconciler.setDamager(dr, AsmPartitionScanner.C_MULTILINE_COMMENT);
reconciler.setRepairer(dr, AsmPartitionScanner.C_MULTILINE_COMMENT);
dr= new DefaultDamagerRepairer(getMultilineCommentScanner());
reconciler.setDamager(dr, AsmPartitionScanner.ASM_MULTILINE_COMMENT);
reconciler.setRepairer(dr, AsmPartitionScanner.ASM_MULTILINE_COMMENT);
dr= new DefaultDamagerRepairer(getSinglelineCommentScanner());
reconciler.setDamager(dr, AsmPartitionScanner.ASM_SINGLE_LINE_COMMENT);
reconciler.setRepairer(dr, AsmPartitionScanner.ASM_SINGLE_LINE_COMMENT);
dr= new DefaultDamagerRepairer(getStringScanner());
reconciler.setDamager(dr, AsmPartitionScanner.ASM_STRING);
reconciler.setRepairer(dr, AsmPartitionScanner.ASM_STRING);
return reconciler;
}
/**
* @see SourceViewerConfiguration#getConfiguredContentTypes(ISourceViewer)
*/
public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
return new String[] { IDocument.DEFAULT_CONTENT_TYPE,
AsmPartitionScanner.ASM_MULTILINE_COMMENT,
AsmPartitionScanner.ASM_SINGLE_LINE_COMMENT,
AsmPartitionScanner.ASM_STRING };
}
}

View file

@ -22,8 +22,10 @@ import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.IEditorInput;
@ -31,7 +33,13 @@ import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.dialogs.SaveAsDialog;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.texteditor.ConvertLineDelimitersAction;
import org.eclipse.ui.texteditor.DefaultRangeIndicator;
import org.eclipse.ui.texteditor.IAbstractTextEditorHelpContextIds;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
import org.eclipse.ui.texteditor.ResourceAction;
import org.eclipse.ui.texteditor.StatusTextEditor;
@ -40,7 +48,7 @@ import org.eclipse.ui.texteditor.DefaultRangeIndicator;
/**
* Assembly text editor
*/
public class AsmTextEditor extends AbstractTextEditor {
public class AsmTextEditor extends StatusTextEditor {
/**
* Creates a new text editor.
@ -126,4 +134,39 @@ public class AsmTextEditor extends AbstractTextEditor {
if (progressMonitor != null)
progressMonitor.setCanceled(!success);
}
/*
* @see AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
* Pulled in from 2.0
*/
protected boolean affectsTextPresentation(PropertyChangeEvent event) {
String p= event.getProperty();
boolean affects= false;
AsmTextTools textTools= CPlugin.getDefault().getAsmTextTools();
affects |= textTools.affectsBehavior(event);
return affects ? affects : super.affectsTextPresentation(event);
}
/*
* @see AbstractTextEditor#createActions()
* @since 2.0
*/
protected void createActions() {
super.createActions();
}
/*
* @see AbstractTextEditor#editorContextMenuAboutToShow(IMenuManager)
* @since 2.0
*/
protected void editorContextMenuAboutToShow(IMenuManager menu) {
super.editorContextMenuAboutToShow(menu);
addAction(menu, ITextEditorActionConstants.GROUP_EDIT, ITextEditorActionConstants.SHIFT_RIGHT);
addAction(menu, ITextEditorActionConstants.GROUP_EDIT, ITextEditorActionConstants.SHIFT_LEFT);
}
}

View file

@ -6,6 +6,8 @@ package org.eclipse.cdt.internal.ui.editor.asm;
*/
import org.eclipse.cdt.internal.ui.CPlugin;
import org.eclipse.cdt.internal.ui.text.ICColorConstants;
import org.eclipse.cdt.internal.ui.text.SingleTokenCScanner;
import org.eclipse.cdt.internal.ui.text.util.CColorManager;
import org.eclipse.jface.preference.IPreferenceStore;
@ -35,7 +37,16 @@ public class AsmTextTools {
private AsmCodeScanner fCodeScanner;
/** The Asm partitions scanner */
private AsmPartitionScanner fPartitionScanner;
/** The ASM multiline comment scanner */
private SingleTokenCScanner fMultilineCommentScanner;
/** The ASM singleline comment scanner */
private SingleTokenCScanner fSinglelineCommentScanner;
/** The ASM string scanner */
private SingleTokenCScanner fStringScanner;
/** The preference store */
private IPreferenceStore fPreferenceStore;
/** The preference change listener */
private PreferenceListener fPreferenceListener= new PreferenceListener();
@ -49,9 +60,14 @@ public class AsmTextTools {
store = CPlugin.getDefault().getPreferenceStore();
}
store.addPropertyChangeListener(fPreferenceListener);
fPreferenceStore = store;
fColorManager= new CColorManager();
fCodeScanner= new AsmCodeScanner(fColorManager, store);
fPartitionScanner= new AsmPartitionScanner();
fMultilineCommentScanner= new SingleTokenCScanner(fColorManager, store, ICColorConstants.C_MULTI_LINE_COMMENT);
fSinglelineCommentScanner= new SingleTokenCScanner(fColorManager, store, ICColorConstants.C_SINGLE_LINE_COMMENT);
fStringScanner= new SingleTokenCScanner(fColorManager, store, ICColorConstants.C_STRING);
}
/**
@ -69,8 +85,20 @@ public class AsmTextTools {
fCodeScanner= null;
fPartitionScanner= null;
fColorManager.dispose();
fColorManager= null;
fMultilineCommentScanner= null;
fSinglelineCommentScanner= null;
fStringScanner= null;
if (fColorManager != null) {
fColorManager.dispose();
fColorManager= null;
}
if (fPreferenceStore != null) {
fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
fPreferenceStore= null;
fPreferenceListener= null;
}
}
/**
@ -100,14 +128,43 @@ public class AsmTextTools {
public IDocumentPartitioner createDocumentPartitioner() {
String[] types= new String[] {
AsmPartitionScanner.C_MULTILINE_COMMENT
AsmPartitionScanner.ASM_MULTILINE_COMMENT,
AsmPartitionScanner.ASM_SINGLE_LINE_COMMENT,
AsmPartitionScanner.ASM_STRING
};
//return new RuleBasedPartitioner(getPartitionScanner(), types);
return new DefaultPartitioner(getPartitionScanner(), types);
}
/**
* Returns a scanner which is configured to scan Java multiline comments.
*
* @return a Java multiline comment scanner
*/
public RuleBasedScanner getMultilineCommentScanner() {
return fMultilineCommentScanner;
}
/**
* Returns a scanner which is configured to scan Java singleline comments.
*
* @return a Java singleline comment scanner
*/
public RuleBasedScanner getSinglelineCommentScanner() {
return fSinglelineCommentScanner;
}
/**
* Returns a scanner which is configured to scan Java strings.
*
* @return a Java string scanner
*/
public RuleBasedScanner getStringScanner() {
return fStringScanner;
}
/**
* Determines whether the preference change encoded by the given event
* changes the behavior of one its contained components.
@ -116,7 +173,10 @@ public class AsmTextTools {
* @return <code>true</code> if event causes a behavioral change
*/
public boolean affectsBehavior(PropertyChangeEvent event) {
return fCodeScanner.affectsBehavior(event);
return fCodeScanner.affectsBehavior(event) ||
fMultilineCommentScanner.affectsBehavior(event) ||
fSinglelineCommentScanner.affectsBehavior(event) ||
fStringScanner.affectsBehavior(event);
}
/**
@ -128,5 +188,12 @@ public class AsmTextTools {
protected void adaptToPreferenceChange(PropertyChangeEvent event) {
if (fCodeScanner.affectsBehavior(event))
fCodeScanner.adaptToPreferenceChange(event);
if (fMultilineCommentScanner.affectsBehavior(event))
fMultilineCommentScanner.adaptToPreferenceChange(event);
if (fSinglelineCommentScanner.affectsBehavior(event))
fSinglelineCommentScanner.adaptToPreferenceChange(event);
if (fStringScanner.affectsBehavior(event))
fStringScanner.adaptToPreferenceChange(event);
}
}