forked from borisbrodski/Jnario
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix sebastianbenz#24 ToekTypePartitioning for richstrings
When doubleclicking a word inside a richstring the whole richstring was selected by the editor. After binding the correct ITokenTypeToPartioningTypeMapper and an appropriate DoubleClickStrategy Richstring-Tokens are handled by finding selecting the word under the courser, instead of selecting the whole token.
- Loading branch information
Showing
6 changed files
with
155 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
...org/jnario/xbase/richstring/ui/editor/XbaseWithRichstringDoubleClickStrategyProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package org.jnario.xbase.richstring.ui.editor; | ||
|
||
|
||
import org.eclipse.jface.text.BadLocationException; | ||
import org.eclipse.jface.text.IDocument; | ||
import org.eclipse.jface.text.IRegion; | ||
import org.eclipse.jface.text.ITextDoubleClickStrategy; | ||
import org.eclipse.jface.text.ITypedRegion; | ||
import org.eclipse.jface.text.Region; | ||
import org.eclipse.jface.text.source.ISourceViewer; | ||
import org.eclipse.xtext.ui.editor.doubleClicking.AbstractPartitionDoubleClickSelector; | ||
import org.eclipse.xtext.ui.editor.doubleClicking.DoubleClickStrategyProvider; | ||
import org.eclipse.xtext.ui.editor.model.CommonBreakIterator; | ||
import org.jnario.xbase.richstring.ui.autoedit.TokenTypeToPartitionMapper; | ||
|
||
/** | ||
* Double click strategy that is aware of rich string partitions. | ||
* | ||
* @author Sebastian Zarnekow - Initial contribution and API | ||
*/ | ||
public class XbaseWithRichstringDoubleClickStrategyProvider extends DoubleClickStrategyProvider { | ||
|
||
@Override | ||
public ITextDoubleClickStrategy getStrategy(ISourceViewer sourceViewer, String contentType, | ||
String documentPartitioning) { | ||
if (TokenTypeToPartitionMapper.RICH_STRING_LITERAL_PARTITION.equals(contentType)) { | ||
return new AbstractPartitionDoubleClickSelector(documentPartitioning) { | ||
|
||
@Override | ||
protected IRegion getSelectedRegion(IDocument document, ITypedRegion completePartition) | ||
throws BadLocationException { | ||
String content = document.get(completePartition.getOffset(), completePartition.getLength()); | ||
int trimLeft = 1; // assume « as start character | ||
if (content.startsWith("'''")) { | ||
trimLeft = 3; | ||
} | ||
int trimRight = 0; | ||
if (content.endsWith("'''")) { | ||
trimRight = 3; | ||
} else if (content.endsWith("''")) { | ||
trimRight = 2; | ||
} else if (content.endsWith("'") || content.endsWith("\u00AB")) { | ||
trimRight = 1; | ||
} | ||
return new Region(completePartition.getOffset() + trimLeft, completePartition.getLength() - trimLeft - trimRight); | ||
} | ||
|
||
}; | ||
} | ||
if (TokenTypeToPartitionMapper.JAVA_DOC_PARTITION.equals(contentType)) { | ||
return new AbstractPartitionDoubleClickSelector(documentPartitioning) { | ||
|
||
/** | ||
* Allows to select the complete <code>@param</code> instead of just the literal <code>param</code>. | ||
* Copied from org.eclipse.jdt.internal.ui.text.java.JavadocDoubleClickStrategy. | ||
*/ | ||
@Override | ||
protected IRegion findExtendedDoubleClickSelection(IDocument document, int position) { | ||
try { | ||
IRegion match = super.findExtendedDoubleClickSelection(document, position); | ||
if (match != null) | ||
return match; | ||
|
||
IRegion word = findWord(document, position); | ||
|
||
IRegion line = document.getLineInformationOfOffset(position); | ||
if (position == line.getOffset() + line.getLength()) | ||
return null; | ||
|
||
int start = word.getOffset(); | ||
int end = start + word.getLength(); | ||
|
||
if (start > 0 | ||
&& document.getChar(start - 1) == '@' | ||
&& Character.isJavaIdentifierPart(document.getChar(start)) | ||
&& (start == 1 || Character.isWhitespace(document.getChar(start - 2)) || document.getChar(start - 2) == '{')) { | ||
// double click after @ident | ||
start--; | ||
} else if (end == position | ||
&& end == start + 1 | ||
&& end < line.getOffset() + line.getLength() | ||
&& document.getChar(end) == '@') { | ||
// double click before " @ident" | ||
return findExtendedDoubleClickSelection(document, position + 1); | ||
} | ||
|
||
if (start == end) | ||
return null; | ||
return new Region(start, end - start); | ||
|
||
} catch (BadLocationException x) { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
protected CommonBreakIterator createBreakIterator() { | ||
return new CommonBreakIterator(false) { | ||
class Braces extends Other { | ||
@Override | ||
protected boolean isValid(char ch) { | ||
return ch == '{' || ch == '}'; | ||
} | ||
} | ||
|
||
class Parentheses extends Other { | ||
@Override | ||
protected boolean isValid(char ch) { | ||
return ch == '(' || ch == ')'; | ||
} | ||
} | ||
|
||
Braces braces = new Braces(); | ||
Parentheses parentheses = new Parentheses(); | ||
|
||
@Override | ||
protected Run getRun(char ch) { | ||
if (braces.isValid(ch)) { | ||
return braces; | ||
} | ||
if (parentheses.isValid(ch)) { | ||
return parentheses; | ||
} | ||
return super.getRun(ch); | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
} | ||
return super.getStrategy(sourceViewer, contentType, documentPartitioning); | ||
} | ||
|
||
} |