use of org.eclipse.jface.text.DocumentCommand in project xtext-eclipse by eclipse.
the class MultiLineTerminalsEditStrategyTest method testFindStopTerminal_3.
/**
* See bug 403812
* If all partitions after the inner closing brace are of type __sl_comment there should be
* a new closing brace as well.
*/
@Test
public void testFindStopTerminal_3() throws Exception {
MultiLineTerminalsEditStrategy strategy = new MultiLineTerminalsEditStrategy("{", "\t", "}", true);
DocumentCommand command = DocumentCommandTest.getDocumentCommandTest(17);
strategy.customizeDocumentCommand(getDocument("Hello {\n Thomas{\n Birthday\n }//}!"), command);
assertEquals("}", command.text.trim());
}
use of org.eclipse.jface.text.DocumentCommand in project xtext-eclipse by eclipse.
the class MultiLineTerminalsEditStrategyTest method testFindStopTerminal.
/**
* See bug 403812
* Closing terminal is already included, so that the command must not contain a new closing brace
*/
@Test
public void testFindStopTerminal() throws Exception {
MultiLineTerminalsEditStrategy strategy = new MultiLineTerminalsEditStrategy("{", "\t", "}", true);
DocumentCommand command = DocumentCommandTest.getDocumentCommandTest(17);
strategy.customizeDocumentCommand(getDocument("Hello {\n Thomas{\n Birthday\n }//\n}!"), command);
assertEquals("", command.text.trim());
}
use of org.eclipse.jface.text.DocumentCommand in project tm4e by eclipse.
the class LanguageConfigurationAutoEditStrategy method customizeDocumentCommand.
@Override
public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
IContentType[] contentTypes = findContentTypes(document);
if (contentTypes == null || command.text.isEmpty()) {
return;
}
installViewer();
if (TextUtils.isEnter(document, command)) {
// key enter pressed
onEnter(document, command);
return;
}
// Auto close pair
LanguageConfigurationRegistryManager registry = LanguageConfigurationRegistryManager.getInstance();
for (IContentType contentType : contentTypes) {
CharacterPair autoClosingPair = registry.getAutoClosePair(document.get(), command.offset, command.text, contentType);
if (autoClosingPair == null) {
continue;
}
command.caretOffset = command.offset + command.text.length();
command.shiftsCaret = false;
if (command.text.equals(autoClosingPair.getKey()) && isFollowedBy(document, command.offset, autoClosingPair.getKey())) {
command.text = "";
} else if (command.text.equals(autoClosingPair.getValue()) && isFollowedBy(document, command.offset, autoClosingPair.getValue())) {
command.text = "";
} else {
command.text += autoClosingPair.getValue();
}
return;
}
Arrays.stream(contentTypes).flatMap(contentType -> registry.getEnabledAutoClosingPairs(contentType).stream()).map(CharacterPair::getValue).filter(command.text::equals).filter(closing -> isFollowedBy(document, command.offset, closing)).findFirst().ifPresent(closing -> {
command.caretOffset = command.offset + command.text.length();
command.shiftsCaret = false;
command.text = "";
});
}
use of org.eclipse.jface.text.DocumentCommand in project Pydev by fabioz.
the class PyEditConfigurationWithoutEditor method getAutoEditStrategies.
/**
* Cache the result, because we'll get asked for it multiple times Now, we always return the PyAutoIndentStrategy. (even on commented lines).
*
* @return PyAutoIndentStrategy which deals with spaces/tabs
*/
@Override
public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String contentType) {
if (autoEditStrategyWrapper == null) {
final PyAutoIndentStrategy pyAutoEditStrategy = getPyAutoIndentStrategy(null);
autoEditStrategyWrapper = new IAutoEditStrategy() {
@Override
public void customizeDocumentCommand(IDocument document, final DocumentCommand command) {
IDocumentCommand wrapper = new IDocumentCommand() {
@Override
public void setText(String string) {
command.text = string;
}
@Override
public void setShiftsCaret(boolean b) {
command.shiftsCaret = b;
}
@Override
public void setOffset(int i) {
command.offset = i;
}
@Override
public void setLength(int i) {
command.length = i;
}
@Override
public void setCaretOffset(int i) {
command.caretOffset = i;
}
@Override
public String getText() {
return command.text;
}
@Override
public int getOffset() {
return command.offset;
}
@Override
public int getLength() {
return command.length;
}
@Override
public boolean getDoIt() {
return command.doit;
}
};
pyAutoEditStrategy.customizeDocumentCommand(document, wrapper);
}
};
}
return new IAutoEditStrategy[] { autoEditStrategyWrapper };
}
use of org.eclipse.jface.text.DocumentCommand in project osate2 by osate.
the class AutoUnindentEditStrategy method internalCustomizeDocumentCommand.
@Override
protected void internalCustomizeDocumentCommand(IDocument document, DocumentCommand command) throws BadLocationException {
if (!isAutoComplete() || !isLineDelimiter(document, command)) {
return;
}
String publicWord = isUseCapitalization() ? "PUBLIC" + System.lineSeparator() + "\t" : "public";
String endWord = isUseCapitalization() ? "END" : "end";
int lineNr = document.getLineOfOffset(command.offset);
int firstOffsetOfLine = document.getLineOffset(lineNr);
int lineLength = document.getLineLength(lineNr);
String lineText = document.get(firstOffsetOfLine, lineLength);
String[] tokens = lineText.trim().split("\\s+");
if (tokens.length < 2) {
return;
}
String keyword = findKeyWord(tokens);
boolean hasExtends = false;
for (int i = 0; i < tokens.length; i++) {
if (tokens[i].equalsIgnoreCase("extends")) {
hasExtends = true;
}
}
NamedElement namedElement = null;
if (keyword.equals("")) {
namedElement = null;
if (document instanceof IXtextDocument) {
IXtextDocument xDoc = (IXtextDocument) document;
namedElement = xDoc.readOnly(resource -> {
EObjectAtOffsetHelper helper = new EObjectAtOffsetHelper();
EObject eobj = helper.resolveElementAt(resource, command.offset);
if (eobj == null) {
return null;
} else if (eobj instanceof Classifier || eobj instanceof AadlPackage) {
return (NamedElement) eobj;
}
return null;
});
}
}
String elementId = findElementId(tokens, keyword);
if (namedElement == null && keyword.equals("") && !hasExtends) {
return;
} else if (checkForExistingEnd(endWord, hasExtends, document.get(), elementId, namedElement, keyword, tokens, firstOffsetOfLine)) {
return;
}
if (ComponentCategory.getByName(elementId.toLowerCase()) != null) {
return;
}
if (keyword == null || keyword.equals("") || elementId == null || elementId.equals("")) {
return;
}
int firstTokenIndex = lineText.indexOf(tokens[0]);
String leadingString = lineText.substring(0, firstTokenIndex);
String indent = "";
String targetString = "";
if (keyword.equalsIgnoreCase("package")) {
if (lineText.endsWith(System.lineSeparator())) {
lineText = lineText.substring(0, lineText.indexOf(System.lineSeparator()));
}
} else if (keyword.equalsIgnoreCase("property set")) {
if (lineText.endsWith(System.lineSeparator())) {
lineText = lineText.substring(0, lineText.indexOf(System.lineSeparator()));
}
if (isAutoIndent()) {
indent = "\t";
}
}
targetString = buildTargetString(lineText, leadingString, endWord, elementId, keyword, publicWord, indent);
if (keyword.equalsIgnoreCase("package")) {
if (isUseCapitalization()) {
command.text = "";
}
command.offset = command.offset + (System.lineSeparator() + leadingString + publicWord).length();
} else if (keyword.equalsIgnoreCase("property set")) {
command.offset = command.offset + (System.lineSeparator() + leadingString + indent).length();
command.text = "";
}
document.replace(firstOffsetOfLine, lineText.length(), targetString);
}
Aggregations