Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

modification for handling :paredit-[forward|backward]-delete function…

…ality
  • Loading branch information...
commit 0ffc000923445e229051f0f37cc35534bec0803a 1 parent ab7eddf
Laurent Petit laurentpetit authored
46 ccw.core/src/ccw/editors/antlrbased/AntlrBasedClojureEditor.java
View
@@ -44,8 +44,8 @@
import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
-import ccw.ClojureCore;
import ccw.CCWPlugin;
+import ccw.ClojureCore;
import ccw.debug.ClojureClient;
import ccw.editors.outline.ClojureOutlinePage;
import ccw.editors.rulesbased.ClojureDocumentProvider;
@@ -86,7 +86,7 @@ public AntlrBasedClojureEditor() {
CCWPlugin.registerEditorColors(preferenceStore);
setSourceViewerConfiguration(new ClojureSourceViewerConfiguration(preferenceStore, this));
setPreferenceStore(preferenceStore);
- setDocumentProvider(new ClojureDocumentProvider());
+ setDocumentProvider(new ClojureDocumentProvider());
}
@Override
@@ -317,7 +317,7 @@ protected IDocument getDocument() {
* Asserts document != null.
* @return
*/
- private int getSourceCaretOffset() {
+ public int getSourceCaretOffset() {
IRegion selection= getSignedSelection(getSourceViewer());
return selection.getOffset() + selection.getLength();
}
@@ -486,7 +486,10 @@ private boolean matchChar(char c, char[] charsToMatch) {
* @param sourceViewer the source viewer
* @return a region denoting the current signed selection, for a resulting RtoL selections length is < 0
*/
- protected IRegion getSignedSelection(ISourceViewer sourceViewer) {
+ public IRegion getSignedSelection(ISourceViewer sourceViewer) {
+ if (sourceViewer == null) {
+ sourceViewer = getSourceViewer();
+ }
StyledText text= sourceViewer.getTextWidget();
Point selection= text.getSelectionRange();
@@ -501,6 +504,22 @@ protected IRegion getSignedSelection(ISourceViewer sourceViewer) {
}
/**
+ * Returns the signed current selection.
+ * The length will be negative if the resulting selection
+ * is right-to-left (RtoL).
+ * <p>
+ * The selection offset is model based.
+ * </p>
+ *
+ * @param sourceViewer the source viewer
+ * @return a region denoting the current signed selection, for a resulting RtoL selections length is < 0
+ */
+ public IRegion getSignedSelection() {
+ return getSignedSelection(null);
+ }
+
+
+ /**
* Returns the unsigned current selection.
* The length will always be positive.
* <p>
@@ -510,7 +529,10 @@ protected IRegion getSignedSelection(ISourceViewer sourceViewer) {
* @param sourceViewer the source viewer
* @return a region denoting the current unsigned selection
*/
- protected IRegion getUnSignedSelection(ISourceViewer sourceViewer) {
+ public IRegion getUnSignedSelection(ISourceViewer sourceViewer) {
+ if (sourceViewer == null) {
+ sourceViewer = getSourceViewer();
+ }
StyledText text= sourceViewer.getTextWidget();
Point selection= text.getSelectionRange();
@@ -519,6 +541,20 @@ protected IRegion getUnSignedSelection(ISourceViewer sourceViewer) {
return new Region(selection.x, selection.y);
}
+ /**
+ * Returns the unsigned current selection.
+ * The length will always be positive.
+ * <p>
+ * The selection offset is model based.
+ * </p>
+ *
+ * @param sourceViewer the source viewer
+ * @return a region denoting the current unsigned selection
+ */
+ public IRegion getUnSignedSelection() {
+ return getUnSignedSelection(null);
+ }
+
/*
* @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#initializeKeyBindingScopes()
*/
3  ccw.core/src/ccw/editors/antlrbased/ClojureSourceViewerConfiguration.java
View
@@ -152,8 +152,9 @@ public IReconciler getReconciler(ISourceViewer sourceViewer) {
@Override
public IAutoEditStrategy[] getAutoEditStrategies(
ISourceViewer sourceViewer, final String contentType) {
+
return new IAutoEditStrategy[] {
- new PareditAutoEditStrategy(fPreferenceStore)
+ new PareditAutoEditStrategy(editor, fPreferenceStore)
, new org.eclipse.jface.text.DefaultIndentLineAutoEditStrategy() };
}
25 ccw.core/src/ccw/editors/antlrbased/PareditAutoEditStrategy.clj
View
@@ -4,15 +4,16 @@
(:import
[org.eclipse.jface.text IAutoEditStrategy
IDocument
- DocumentCommand])
+ DocumentCommand]
+ [ccw.editors.antlrbased AntlrBasedClojureEditor])
(:gen-class
:implements [org.eclipse.jface.text.IAutoEditStrategy]
- :constructors {[org.eclipse.jface.preference.IPreferenceStore] []}
+ :constructors {[ccw.editors.antlrbased.AntlrBasedClojureEditor org.eclipse.jface.preference.IPreferenceStore] []}
:init init
:state state))
(defn- -init
- [preference-store] [[] (ref {:prefs-store preference-store})])
+ [editor preference-store] [[] (ref {:editor editor :prefs-store preference-store})])
; TODO move this into paredit itself ...
(def *one-char-command*
@@ -26,18 +27,20 @@
(defn- call-paredit [command document-text]
(cond
- (and (zero? (:length command))
+ (and (zero? (:length command)) ; TODO enhance to also handle the replace of a bunch of text
(contains? *one-char-command* (:text command)))
(paredit (get *one-char-command* (:text command))
- {:text document-text
+ {:text (:text document-text)
:offset (:offset command)
:length 0})
(and (zero? (-> command :text .length))
(= 1 (:length command)))
- (paredit :paredit-forward-delete ; TODO how to distinguish from forward delete and backward delete ?
- {:text document-text
- :offset (:offset command)
- :length 1})))
+ (let [paredit-command (if (= (:offset command) (:caret-offset document-text))
+ :paredit-forward-delete :paredit-backward-delete)]
+ (paredit paredit-command
+ {:text (:text document-text)
+ :offset (if (= paredit-command :paredit-backward-delete) (inc (:offset command)) (:offset command))
+ :length 1}))))
(defn paredit-enabled?
@@ -48,7 +51,9 @@
[#^IAutoEditStrategy this, #^IDocument document, #^DocumentCommand command]
(when (and (paredit-enabled? (-> this .state deref :prefs-store))
(.doit command))
- (let [document-text (.get document)
+
+ (let [signed-selection (bean (-> this .state deref :editor .getSignedSelection))
+ document-text {:text (.get document) :caret-offset (+ (:offset signed-selection) (:length signed-selection)) :selection-length (:length signed-selection)}
par-command {:text (.text command) :offset (.offset command) :length (.length command)}
result (call-paredit par-command document-text)]
(when (and result (not= :ko (-> result :parser-state)))
Please sign in to comment.
Something went wrong with that request. Please try again.