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

Commit

Permalink
LANG: intermediate refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-medeiros committed Sep 28, 2015
1 parent 08b762d commit 0116a5e
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 59 deletions.
Expand Up @@ -23,9 +23,7 @@
import melnorme.lang.ide.core.text.BlockHeuristicsScannner;
import melnorme.lang.ide.core.text.SamplePartitionScanner;
import melnorme.lang.ide.core.text.Scanner_BaseTest;
import melnorme.lang.ide.ui.CodeFormatterConstants;
import melnorme.lang.ide.ui.CodeFormatterConstants.IndentMode;
import melnorme.lang.ide.ui.LangAutoEditPreferenceConstants;
import melnorme.lang.ide.ui.text.util.LangAutoEditUtils;
import melnorme.utilbox.misc.MiscUtil;

Expand All @@ -45,8 +43,41 @@ public class LangAutoEditStrategyTest extends Scanner_BaseTest {

protected LangAutoEditStrategy getAutoEditStrategy() {
if(autoEditStrategy == null) {
setPreferences();
autoEditStrategy = new LangAutoEditStrategy(null) {

ILangAutoEditsPreferencesAccess preferences = new LangAutoEditsPreferencesAccess() {
@Override
public boolean isSmartIndent() {
return true;
}
@Override
public boolean isSmartDeIndent() {
return true;
}
@Override
public boolean closeBraces() {
return true;
}
@Override
public boolean closeBlocks() {
return true;
}

@Override
public boolean isSmartPaste() {
return true;
}

@Override
public IndentMode getTabStyle() {
return IndentMode.TAB;
}
@Override
public int getIndentSize() {
return 4;
}
};

autoEditStrategy = new LangAutoEditStrategy(null, preferences) {
@Override
protected BlockHeuristicsScannner createBlockHeuristicsScanner(IDocument doc) {
return Scanner_BaseTest.createBlockHeuristicScannerWithSamplePartitioning(doc);
Expand All @@ -56,19 +87,6 @@ protected BlockHeuristicsScannner createBlockHeuristicsScanner(IDocument doc) {
return autoEditStrategy;
}

protected void setPreferences() {
LangAutoEditPreferenceConstants.AE_SMART_INDENT.set(true);
LangAutoEditPreferenceConstants.AE_SMART_DEINDENT.set(true);
LangAutoEditPreferenceConstants.AE_PARENTHESES_AS_BLOCKS.set(true);
LangAutoEditPreferenceConstants.AE_CLOSE_BRACES.set(true);
LangAutoEditPreferenceConstants.AE_CLOSE_BRACKETS.set(true);
LangAutoEditPreferenceConstants.AE_CLOSE_STRINGS.set(true);
LangAutoEditPreferenceConstants.AE_SMART_PASTE.set(true);

CodeFormatterConstants.FORMATTER_TAB_SIZE.set(4);
CodeFormatterConstants.FORMATTER_INDENT_MODE.set(IndentMode.TAB.getPrefValue());
}

protected DocumentCommand createDocumentCommand(int start, int length, String text) {
DocumentCommand documentCommand = new DocumentCommand() {};
documentCommand.doit = true;
Expand Down
@@ -0,0 +1,41 @@
/*******************************************************************************
* Copyright (c) 2015 Bruno Medeiros and other Contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.ide.ui.editor.text;

import melnorme.lang.ide.ui.CodeFormatterConstants.IndentMode;
import melnorme.lang.ide.ui.text.util.AutoEditUtils;


public interface ILangAutoEditsPreferencesAccess {

boolean isSmartIndent();

boolean isSmartDeIndent();

boolean closeBlocks();

boolean closeBraces();

boolean isSmartPaste();

IndentMode getTabStyle();

int getIndentSize();

default String getIndentUnit() {
if (getTabStyle() == IndentMode.SPACES) {
return AutoEditUtils.getNSpaces(getIndentSize());
} else {
return "\t";
}
}

}
Expand Up @@ -17,6 +17,7 @@
import melnorme.lang.ide.core.text.BlockHeuristicsScannner;
import melnorme.lang.ide.core.text.BlockHeuristicsScannner.BlockBalanceResult;
import melnorme.lang.ide.core.text.BlockHeuristicsScannner.BlockTokenRule;
import melnorme.lang.ide.ui.LangUIPlugin;
import melnorme.lang.ide.ui.text.util.AutoEditUtils;
import melnorme.lang.ide.ui.text.util.LangAutoEditUtils;

Expand Down Expand Up @@ -46,15 +47,16 @@
*/
public class LangAutoEditStrategy extends DefaultIndentLineAutoEditStrategy {

protected final LangAutoEditsPreferencesAdapter fPreferences;

protected boolean fIsSmartMode;
protected boolean fCloseBlocks;
protected final ILangAutoEditsPreferencesAccess preferences;

protected Event lastKeyEvent;

public LangAutoEditStrategy(ITextViewer viewer) {
this.fPreferences = new LangAutoEditsPreferencesAdapter();
this(viewer, new LangAutoEditsPreferencesAccess());
}

public LangAutoEditStrategy(ITextViewer viewer, ILangAutoEditsPreferencesAccess preferences) {
this.preferences = preferences;

lastKeyEvent = new Event();
if (viewer instanceof ITextViewerExtension) {
Expand Down Expand Up @@ -83,45 +85,32 @@ protected boolean keyWasDelete() {
return lastKeyEvent.character == SWT.DEL;
}

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

protected void clearCachedValues() {
fCloseBlocks = fPreferences.closeBlocks();
fIsSmartMode = fPreferences.isSmartMode();
}

protected boolean isSmartMode() {
return fIsSmartMode;
}
protected String indentUnit;

@Override
public void customizeDocumentCommand(IDocument doc, DocumentCommand cmd) {
if (cmd.doit == false)
return;

clearCachedValues();
if(!isSmartMode()) {
super.customizeDocumentCommand(doc, cmd);
return;
}
boolean isSmartIndent = preferences.isSmartIndent();
indentUnit = preferences.getIndentUnit();

try {
if(AutoEditUtils.isNewLineInsertionCommand(doc, cmd)) {
if(isSmartIndent && AutoEditUtils.isNewLineInsertionCommand(doc, 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)) {
} else if(isSmartIndent && AutoEditUtils.isSingleCharactedInsertionOrReplaceCommand(cmd)) {
smartIndentOnKeypress(doc, cmd);
} else if(cmd.text.length() > 1 && fPreferences.isSmartPaste()) {
} else if(preferences.isSmartPaste() && cmd.text.length() > 1) {
smartPaste(doc, cmd); // no smart backspace for paste
} else {
super.customizeDocumentCommand(doc, cmd);
}
} catch (BadLocationException e) {
//DLTKUIPlugin.log(e);
throw melnorme.utilbox.core.ExceptionAdapter.unchecked(e);
LangUIPlugin.logError("Error in LangAutoEditStrategy", e);
}
}

Expand Down Expand Up @@ -151,7 +140,7 @@ protected void smartIndentAfterNewLine(IDocument doc, DocumentCommand cmd) throw

int postWsEndPos = AutoEditUtils.findEndOfWhiteSpace(doc, cmd.offset, lineEnd);
boolean hasPendingTextAfterEdit = postWsEndPos != lineEnd;
if (fCloseBlocks && !hasPendingTextAfterEdit){
if(preferences.closeBlocks() && !hasPendingTextAfterEdit){
if(bhscanner.shouldCloseBlock(blockInfo.rightmostUnbalancedBlockOpenOffset)) {
//close block
cmd.caretOffset = cmd.offset + cmd.text.length();
Expand Down Expand Up @@ -254,13 +243,13 @@ protected static String getLineIndent(IDocument doc, int start, int end) throws
}

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

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

protected boolean smartDeIndentAfterDeletion(IDocument doc, DocumentCommand cmd) throws BadLocationException {
if(!fPreferences.isSmartDeIndent())
if(!preferences.isSmartDeIndent())
return false;

if(!cmd.text.isEmpty())
Expand Down Expand Up @@ -370,7 +359,7 @@ protected void smartPaste(IDocument doc, DocumentCommand cmd) throws BadLocation
}

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

Expand Down
Expand Up @@ -14,47 +14,44 @@
import melnorme.lang.ide.ui.CodeFormatterConstants;
import melnorme.lang.ide.ui.CodeFormatterConstants.IndentMode;
import melnorme.lang.ide.ui.LangAutoEditPreferenceConstants;
import melnorme.lang.ide.ui.text.util.AutoEditUtils;

// Originally was RubyPreferencesInterpreter from DLTK
public class LangAutoEditsPreferencesAdapter {
public class LangAutoEditsPreferencesAccess implements ILangAutoEditsPreferencesAccess {

public LangAutoEditsPreferencesAdapter() {
public LangAutoEditsPreferencesAccess() {
}

public boolean isSmartMode() {
@Override
public boolean isSmartIndent() {
return LangAutoEditPreferenceConstants.AE_SMART_INDENT.get();
}

@Override
public boolean isSmartDeIndent() {
return LangAutoEditPreferenceConstants.AE_SMART_DEINDENT.get();
}

@Override
public boolean closeBlocks() {
return closeBraces();
}

@Override
public boolean closeBraces() {
return LangAutoEditPreferenceConstants.AE_CLOSE_BRACES.get();
}

@Override
public boolean isSmartPaste() {
return LangAutoEditPreferenceConstants.AE_SMART_PASTE.get();
}


@Override
public IndentMode getTabStyle() {
return IndentMode.fromPrefStore();
}

public String getIndentUnit() {
if (getTabStyle() == IndentMode.SPACES) {
return AutoEditUtils.getNSpaces(getIndentSize());
} else {
return "\t";
}
}

@Override
public int getIndentSize() {
return CodeFormatterConstants.FORMATTER_INDENTATION_SPACES_SIZE.get();
}
Expand Down

0 comments on commit 0116a5e

Please sign in to comment.