Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

Commit

Permalink
LANG: fix indentation when TAB pressed.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-medeiros committed May 28, 2015
1 parent 204b368 commit 20c87fb
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 37 deletions.
1 change: 1 addition & 0 deletions documentation/ChangeLog.md
Expand Up @@ -2,6 +2,7 @@
[Latest features on top]

### (NextVersion)
* Fixed: `Tab policy: "Spaces Only"` preference ignored when pressing TAB.
* Added Content Assist preference page, with auto-activation options.
* Fixed: the preference pages are now searchable in the Preferences dialog search field, by means of relevant keywords.

Expand Down
Expand Up @@ -10,6 +10,7 @@
*******************************************************************************/
package melnorme.lang.ide.core.utils.prefs;

import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;
import static melnorme.utilbox.core.Assert.AssertNamespace.assertTrue;

import java.util.HashMap;
Expand All @@ -21,7 +22,7 @@ public class AbstractPreferenceHelper {
public final String key;

public AbstractPreferenceHelper(String key) {
this.key = key;
this.key = assertNotNull(key);

synchronized (instances) {
// Allow only one instance of a preference helper per key.
Expand Down
Expand Up @@ -90,6 +90,12 @@ public IEclipsePreferences getProjectNode(IProject project) {
return new ProjectScope(project).getNode(getQualifier());
}

/* ----------------- util ----------------- */

public boolean keyEquals(String otherKey) {
return otherKey!= null && key.equals(otherKey);
}

/* ----------------- listeners ----------------- */

public IPreferenceChangeListener_Ext addPrefChangeListener(boolean initializeChange,
Expand Down
@@ -1,19 +1,13 @@
package melnorme.lang.ide.ui;

import static melnorme.utilbox.core.CoreUtil.array;

public interface CodeFormatterConstants extends CodeFormatterConstants_Actual {

public final String[] FORMATTER_TAB_CHAR__VALUES = array(
TAB, SPACES, MIXED
);

// helper for tab indent mode
public enum IndentMode {

TAB(CodeFormatterConstants.TAB),
SPACES(CodeFormatterConstants.SPACES),
MIXED(CodeFormatterConstants.MIXED);
SPACES(CodeFormatterConstants.SPACES);

protected final String prefValue;

Expand Down
Expand Up @@ -155,8 +155,8 @@ protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
@Override
protected boolean affectsTextPresentation(PropertyChangeEvent event) {
AbstractLangSourceViewerConfiguration langSVC = getSourceViewerConfiguration_asLang();
if(langSVC != null) {
return langSVC.affectsTextPresentation(event) || super.affectsTextPresentation(event);
if(langSVC != null && langSVC.affectsTextPresentation(event)) {
return true;
}

return super.affectsTextPresentation(event);
Expand Down Expand Up @@ -202,6 +202,13 @@ public void createPartControl(Composite parent) {
editorSelChangedListener.install(getSelectionProvider());
}

/* ----------------- other ----------------- */

@Override
protected boolean isTabsToSpacesConversionEnabled() {
return false;
}

protected final AbstractSelectionChangedListener editorSelChangedListener = new AbstractSelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
Expand Down
Expand Up @@ -13,9 +13,10 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.util.PropertyChangeEvent;

public interface ISourceViewerExt {
public interface ISourceViewerExt extends ISourceViewer {

IContentAssistant getContentAssistant();

Expand Down
Expand Up @@ -11,8 +11,9 @@
package melnorme.lang.ide.ui.editor;


import static melnorme.utilbox.core.Assert.AssertNamespace.assertTrue;
import melnorme.lang.ide.core.LangCore;
import melnorme.lang.ide.ui.text.AbstractLangSourceViewerConfiguration;
import melnorme.lang.ide.ui.text.SimpleLangSourceViewerConfiguration;
import melnorme.lang.ide.ui.text.completion.ContentAssistantExt;
import melnorme.utilbox.collections.ArrayList2;
import melnorme.utilbox.ownership.IDisposable;
Expand Down Expand Up @@ -43,37 +44,42 @@ public <T extends IDisposable> T addConfigurationOwned(T ownedObject) {
return ownedObject;
}

@Override
public void unconfigure() {
for(IDisposable disposable : configurationDisposables) {
disposable.dispose();
}
configurationDisposables.clear();

super.unconfigure();
}

@Override
protected void configure_beforeViewerColors(SourceViewerConfiguration configuration) {
super.configure_beforeViewerColors(configuration);
}
protected SimpleLangSourceViewerConfiguration sourceViewerConfig;

@Override
public void configure(SourceViewerConfiguration configuration) {
assertTrue(sourceViewerConfig == null);

super.configure(configuration);

if(configuration instanceof AbstractLangSourceViewerConfiguration) {
AbstractLangSourceViewerConfiguration langConfig = (AbstractLangSourceViewerConfiguration) configuration;
if(configuration instanceof SimpleLangSourceViewerConfiguration) {
sourceViewerConfig = (SimpleLangSourceViewerConfiguration) configuration;

StyledText textWidget = getTextWidget();
if (textWidget != null) {
textWidget.setFont(JFaceResources.getFont(langConfig.getFontPropertyPreferenceKey()));
textWidget.setFont(JFaceResources.getFont(sourceViewerConfig.getFontPropertyPreferenceKey()));
}

langConfig.setupCustomConfiguration(this);
sourceViewerConfig.setupCustomConfiguration(this);
}
}

@Override
protected void configure_beforeViewerColors(SourceViewerConfiguration configuration) {
super.configure_beforeViewerColors(configuration);
}

@Override
public void unconfigure() {
for(IDisposable disposable : configurationDisposables) {
disposable.dispose();
}
configurationDisposables.clear();

super.unconfigure();
sourceViewerConfig = null;
}

/* ----------------- ----------------- */

@Override
Expand Down Expand Up @@ -123,6 +129,8 @@ public IContentAssistant getContentAssistant() {

@Override
public void handlePropertyChangeEvent_2(PropertyChangeEvent event, IPreferenceStore prefStore) {
sourceViewerConfig.reconfigureForPropertyChange(event, prefStore, this);

if(getContentAssistant() instanceof ContentAssistantExt) {
ContentAssistantExt caExt = (ContentAssistantExt) getContentAssistant();
caExt.handlePrefChange(event, prefStore);
Expand Down
Expand Up @@ -13,6 +13,7 @@

import static melnorme.utilbox.core.Assert.AssertNamespace.assertTrue;
import static melnorme.utilbox.core.Assert.AssertNamespace.assertUnreachable;
import static melnorme.utilbox.core.CoreUtil.areEqual;
import melnorme.lang.ide.core.text.BlockHeuristicsScannner;
import melnorme.lang.ide.core.text.BlockHeuristicsScannner.BlockBalanceResult;
import melnorme.lang.ide.core.text.BlockHeuristicsScannner.BlockTokenRule;
Expand Down Expand Up @@ -109,6 +110,8 @@ public void customizeDocumentCommand(IDocument doc, DocumentCommand cmd) {
smartIndentAfterNewLine(doc, cmd);
} else if(smartDeIndentAfterDeletion(doc, cmd)) {
return;
} else if(lastKeyEvent.character == SWT.TAB && areEqual(cmd.text, "\t")) {
smartTab(doc, cmd);
} else if(AutoEditUtils.isSingleCharactedInsertionOrReplaceCommand(cmd)) {
smartIndentOnKeypress(doc, cmd);
} else if(cmd.text.length() > 1 && fPreferences.isSmartPaste()) {
Expand Down Expand Up @@ -251,7 +254,7 @@ protected static String getLineIndent(IDocument doc, int start, int end) throws
}

protected String addIndent(String indentStr, int indentDelta) {
return indentStr + LangAutoEditUtils.stringNTimes(fPreferences.getIndent(), indentDelta);
return indentStr + LangAutoEditUtils.stringNTimes(fPreferences.getIndentUnit(), indentDelta);
}

/* ------------------------------------- */
Expand Down Expand Up @@ -365,4 +368,10 @@ protected void smartIndentOnKeypress(IDocument doc, DocumentCommand cmd) throws
protected void smartPaste(IDocument doc, DocumentCommand cmd) throws BadLocationException {
super.customizeDocumentCommand(doc, cmd);
}

protected void smartTab(IDocument doc, DocumentCommand cmd) {
cmd.text = fPreferences.getIndentUnit();
super.customizeDocumentCommand(doc, cmd);
}

}
Expand Up @@ -46,7 +46,7 @@ public IndentMode getTabStyle() {
return IndentMode.fromPrefStore();
}

public String getIndent() {
public String getIndentUnit() {
if (getTabStyle() == IndentMode.SPACES) {
return AutoEditUtils.getNSpaces(getIndentSize());
} else {
Expand Down
Expand Up @@ -17,6 +17,6 @@ public interface FormatterMessages {
public static String IndentationGroup_tab_size =
"Displayed tab &size:";
public static String IndentationGroup_indent_size =
"&Indentation size:";
"&Indentation size (for spaces):";

}
Expand Up @@ -10,11 +10,14 @@
*******************************************************************************/
package melnorme.lang.ide.ui.text;

import static melnorme.utilbox.core.Assert.AssertNamespace.assertUnreachable;
import static melnorme.utilbox.core.CoreUtil.array;
import static melnorme.utilbox.core.CoreUtil.tryCast;

import java.util.Map;

import melnorme.lang.ide.ui.CodeFormatterConstants;
import melnorme.lang.ide.ui.CodeFormatterConstants.IndentMode;
import melnorme.lang.ide.ui.EditorSettings_Actual;
import melnorme.lang.ide.ui.LangUIPlugin;
import melnorme.lang.ide.ui.LangUIPlugin_Actual;
Expand All @@ -28,6 +31,7 @@
import melnorme.lang.ide.ui.text.completion.ContentAssistantExt;
import melnorme.lang.ide.ui.text.completion.LangContentAssistProcessor;
import melnorme.lang.ide.ui.text.completion.LangContentAssistProcessor.ContentAssistCategoriesBuilder;
import melnorme.lang.ide.ui.text.util.AutoEditUtils;
import melnorme.utilbox.collections.Indexable;

import org.eclipse.cdt.ui.text.IColorManager;
Expand All @@ -44,6 +48,7 @@
import org.eclipse.jface.text.reconciler.AbstractReconciler;
import org.eclipse.jface.text.reconciler.IReconciler;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditor;
import org.eclipse.ui.texteditor.ITextEditor;

Expand All @@ -66,7 +71,6 @@ public AbstractDecoratedTextEditor getEditor() {
public AbstractLangEditor getEditor_asLang() {
return tryCast(editor, AbstractLangEditor.class);
}


/* ----------------- Navigation operations ----------------- */

Expand Down Expand Up @@ -103,6 +107,13 @@ protected IInformationControlCreator getOutlinePresenterControlCreator(

/* ----------------- Modification operations ----------------- */

@Override
public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
return new String[] { getToggleCommentPrefix(), "" };
}

protected abstract String getToggleCommentPrefix();

@Override
public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String contentType) {
if(IDocument.DEFAULT_CONTENT_TYPE.equals(contentType)) {
Expand All @@ -113,11 +124,34 @@ public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, Str
}

@Override
public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
return new String[] { getToggleCommentPrefix(), "" };
public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {

IndentMode indentMode = CodeFormatterConstants.IndentMode.fromPrefStore();
int spaceIndentationSize = CodeFormatterConstants.FORMATTER_INDENTATION_SPACES_SIZE.get();
String spaceIndent = AutoEditUtils.getNSpaces(spaceIndentationSize);

switch (indentMode) {
case TAB: return array("\t", spaceIndent); // return getIndentPrefixesForTab(spaceIndent);
case SPACES: return array(spaceIndent, "\t"); // return getIndentPrefixesForSpaces(spaceIndent);
}

throw assertUnreachable();
}

protected abstract String getToggleCommentPrefix();
@Override
protected void updateIndentationSettings(SourceViewer sourceViewer, String property) {
super.updateIndentationSettings(sourceViewer, property);

if(
CodeFormatterConstants.FORMATTER_INDENTATION_SPACES_SIZE.keyEquals(property) ||
CodeFormatterConstants.FORMATTER_INDENT_MODE.keyEquals(property)) {

for(String contentType : getConfiguredContentTypes(sourceViewer)) {
String[] prefixes= getIndentPrefixes(sourceViewer, contentType);
sourceViewer.setIndentPrefixes(prefixes, contentType);
}
}
}

/* ----------------- Content Assist ----------------- */

Expand Down
Expand Up @@ -21,6 +21,7 @@
import java.util.Map.Entry;

import melnorme.lang.ide.core.TextSettings_Actual;
import melnorme.lang.ide.ui.CodeFormatterConstants;
import melnorme.lang.ide.ui.LangUIMessages;
import melnorme.lang.ide.ui.editor.LangSourceViewer;
import melnorme.lang.ide.ui.text.coloring.AbstractLangScanner;
Expand All @@ -44,6 +45,7 @@
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Shell;
Expand Down Expand Up @@ -81,6 +83,15 @@ public IPreferenceStore getPreferenceStore() {
return preferenceStore;
}

public void reconfigureForPropertyChange(PropertyChangeEvent event, IPreferenceStore prefStore,
SourceViewer sourceViewer) {
assertTrue(prefStore == getPreferenceStore());
String property = event.getProperty();
updateIndentationSettings(sourceViewer, property);
}

/* ----------------- scanners / partitioning ----------------- */

@Override
public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
return TextSettings_Actual.PARTITIONING_ID;
Expand Down Expand Up @@ -180,6 +191,23 @@ public String getFontPropertyPreferenceKey() {
return JFaceResources.TEXT_FONT;
}

/* ----------------- TextViewer ----------------- */

@Override
public int getTabWidth(ISourceViewer sourceViewer) {
if (fPreferenceStore == null)
return super.getTabWidth(sourceViewer);
return fPreferenceStore.getInt(CodeFormatterConstants.FORMATTER_TAB_SIZE.key);
}

protected void updateIndentationSettings(SourceViewer sourceViewer, String property) {
if(CodeFormatterConstants.FORMATTER_TAB_SIZE.keyEquals(property)) {
StyledText textWidget = sourceViewer.getTextWidget();
int tabWidth = getTabWidth(sourceViewer);
textWidget.setTabs(tabWidth);
}
}

/* ----------------- Presentation UI controls ----------------- */

@Override
Expand Down

0 comments on commit 20c87fb

Please sign in to comment.