From 0116a5e6ec7fc1d3ece1805530794dfc3dacc982 Mon Sep 17 00:00:00 2001 From: Bruno Medeiros Date: Mon, 28 Sep 2015 18:36:56 +0100 Subject: [PATCH] LANG: intermediate refactoring --- .../editor/text/LangAutoEditStrategyTest.java | 52 +++++++++++++------ .../text/ILangAutoEditsPreferencesAccess.java | 41 +++++++++++++++ .../ui/editor/text/LangAutoEditStrategy.java | 47 +++++++---------- ...va => LangAutoEditsPreferencesAccess.java} | 23 ++++---- 4 files changed, 104 insertions(+), 59 deletions(-) create mode 100644 plugin_ide.ui/src-lang/melnorme/lang/ide/ui/editor/text/ILangAutoEditsPreferencesAccess.java rename plugin_ide.ui/src-lang/melnorme/lang/ide/ui/editor/text/{LangAutoEditsPreferencesAdapter.java => LangAutoEditsPreferencesAccess.java} (76%) diff --git a/plugin_ide.ui.tests/src-lang/melnorme/lang/ide/ui/editor/text/LangAutoEditStrategyTest.java b/plugin_ide.ui.tests/src-lang/melnorme/lang/ide/ui/editor/text/LangAutoEditStrategyTest.java index 7d4807daf..d17288643 100644 --- a/plugin_ide.ui.tests/src-lang/melnorme/lang/ide/ui/editor/text/LangAutoEditStrategyTest.java +++ b/plugin_ide.ui.tests/src-lang/melnorme/lang/ide/ui/editor/text/LangAutoEditStrategyTest.java @@ -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; @@ -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); @@ -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; diff --git a/plugin_ide.ui/src-lang/melnorme/lang/ide/ui/editor/text/ILangAutoEditsPreferencesAccess.java b/plugin_ide.ui/src-lang/melnorme/lang/ide/ui/editor/text/ILangAutoEditsPreferencesAccess.java new file mode 100644 index 000000000..bb688054a --- /dev/null +++ b/plugin_ide.ui/src-lang/melnorme/lang/ide/ui/editor/text/ILangAutoEditsPreferencesAccess.java @@ -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"; + } + } + +} \ No newline at end of file diff --git a/plugin_ide.ui/src-lang/melnorme/lang/ide/ui/editor/text/LangAutoEditStrategy.java b/plugin_ide.ui/src-lang/melnorme/lang/ide/ui/editor/text/LangAutoEditStrategy.java index 975836343..bbdc80662 100644 --- a/plugin_ide.ui/src-lang/melnorme/lang/ide/ui/editor/text/LangAutoEditStrategy.java +++ b/plugin_ide.ui/src-lang/melnorme/lang/ide/ui/editor/text/LangAutoEditStrategy.java @@ -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; @@ -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) { @@ -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); } } @@ -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(); @@ -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()) @@ -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); } diff --git a/plugin_ide.ui/src-lang/melnorme/lang/ide/ui/editor/text/LangAutoEditsPreferencesAdapter.java b/plugin_ide.ui/src-lang/melnorme/lang/ide/ui/editor/text/LangAutoEditsPreferencesAccess.java similarity index 76% rename from plugin_ide.ui/src-lang/melnorme/lang/ide/ui/editor/text/LangAutoEditsPreferencesAdapter.java rename to plugin_ide.ui/src-lang/melnorme/lang/ide/ui/editor/text/LangAutoEditsPreferencesAccess.java index 9c81450f4..dc47cc160 100644 --- a/plugin_ide.ui/src-lang/melnorme/lang/ide/ui/editor/text/LangAutoEditsPreferencesAdapter.java +++ b/plugin_ide.ui/src-lang/melnorme/lang/ide/ui/editor/text/LangAutoEditsPreferencesAccess.java @@ -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(); }