use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position in project rstudio by rstudio.
the class CppCompletionRequest method applyValue.
private void applyValue(CppCompletion completion) {
if (invalidationToken_.isInvalid())
return;
terminate();
if (completion.getType() == CppCompletion.SNIPPET) {
snippets_.applySnippet(getUserTypedText(), completion.getTypedText());
return;
}
String insertText = completion.getTypedText();
if (completion.getType() == CppCompletion.FUNCTION && uiPrefs_.insertParensAfterFunctionCompletion().getValue()) {
if (uiPrefs_.insertMatching().getValue())
insertText = insertText + "()";
else
insertText = insertText + "(";
} else if (completion.getType() == CppCompletion.DIRECTORY) {
insertText += "/";
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
manager_.codeCompletion();
}
});
}
docDisplay_.setFocus(true);
docDisplay_.setSelection(getReplacementSelection());
docDisplay_.replaceSelection(insertText, true);
if (completion.hasParameters() && uiPrefs_.insertParensAfterFunctionCompletion().getValue() && uiPrefs_.insertMatching().getValue()) {
Position pos = docDisplay_.getCursorPosition();
pos = Position.create(pos.getRow(), pos.getColumn() - 1);
docDisplay_.setSelectionRange(Range.fromPoints(pos, pos));
} else if (completion.getType() == CppCompletion.FILE) {
char ch = docDisplay_.getCharacterAtCursor();
// if there is a '>' or '"' following the cursor, move over it
if (ch == '>' || ch == '"') {
docDisplay_.moveCursorForward();
} else // otherwise, insert the corresponding closing character
{
String line = docDisplay_.getCurrentLine();
if (line.contains("<"))
docDisplay_.insertCode(">");
else if (line.contains("\""))
docDisplay_.insertCode("\"");
}
}
if (completion.hasParameters() && uiPrefs_.showSignatureTooltips().getValue()) {
new CppCompletionSignatureTip(completion, docDisplay_);
}
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position in project rstudio by rstudio.
the class CppCompletionSignatureTip method onLoad.
@Override
protected void onLoad() {
super.onLoad();
allTips_.add(this);
nativePreviewReg_ = Event.addNativePreviewHandler(new NativePreviewHandler() {
public void onPreviewNativeEvent(NativePreviewEvent e) {
if (e.getTypeInt() == Event.ONKEYDOWN) {
// handle keys
switch(e.getNativeEvent().getKeyCode()) {
case KeyCodes.KEY_ESCAPE:
e.cancel();
hide();
break;
case KeyCodes.KEY_DOWN:
e.cancel();
setTextIndex(currentTextIndex_ + 1);
break;
case KeyCodes.KEY_UP:
e.cancel();
setTextIndex(currentTextIndex_ - 1);
break;
}
// dismiss if we've left our anchor zone
// (defer this so the current key has a chance to
// enter the editor and affect the cursor)
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
Position cursorPos = docDisplay_.getCursorPosition();
Range anchorRange = anchor_.getRange();
if (cursorPos.isBeforeOrEqualTo(anchorRange.getStart()) || cursorPos.isAfterOrEqualTo(anchorRange.getEnd())) {
hide();
}
}
});
}
}
});
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position in project rstudio by rstudio.
the class CppCompletionUtils method getCompletionPosition.
public static CompletionPosition getCompletionPosition(DocDisplay docDisplay, boolean explicit, boolean alwaysComplete, int autoChars) {
// get the current line of code
String line = docDisplay.getCurrentLine();
// is this an '#include' line?
Pattern reInclude = Pattern.create("^\\s*#+\\s*include", "");
boolean isInclude = reInclude.test(line);
// get the cursor position
Position position = docDisplay.getCursorPosition();
// if so then bail
if ((position.getColumn() < line.length()) && CppCompletionUtils.isCppIdentifierChar(line.charAt(position.getColumn()))) {
return null;
}
// determine the column right before this one
int inputCol = position.getColumn() - 1;
// walk backwards across C++ identifer symbols
int col = inputCol;
if (isInclude) {
while (col >= 0 && !StringUtil.isOneOf(line.charAt(col), '/', '<', '"')) {
col--;
}
} else {
while ((col >= 0) && CppCompletionUtils.isCppIdentifierChar(line.charAt(col))) {
col--;
}
}
// record source position
Position startPos = Position.create(position.getRow(), col + 1);
// check for a completion triggering sequence
char ch = line.charAt(col);
char prefixCh = line.charAt(col - 1);
// member
if (ch == '.' || (prefixCh == '-' && ch == '>')) {
return new CompletionPosition(startPos, // no user text (get all completions)
"", CompletionPosition.Scope.Member);
} else // scope
if (prefixCh == ':' && ch == ':') {
return new CompletionPosition(startPos, // no user text (get all completions)
"", CompletionPosition.Scope.Namespace);
} else // directory
if (isInclude && ch == '/') {
return new CompletionPosition(startPos, // no user test (get all completions)
"", CompletionPosition.Scope.File);
} else // minimum character threshold
if (// either always completing or explicit
(alwaysComplete || explicit) && // meets the character threshold
((inputCol - col) >= (explicit ? 1 : autoChars)) && // not a quote character
(isInclude || ch != '"')) {
// calculate user text (up to two characters of additional
// server side filter)
String userText = line.substring(col + 1, Math.min(col + 3, position.getColumn()));
CompletionPosition.Scope scope = isInclude ? CompletionPosition.Scope.File : CompletionPosition.Scope.Global;
return new CompletionPosition(startPos, userText, scope);
} else {
return null;
}
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position in project rstudio by rstudio.
the class ChunkContextUi method switchChunk.
@Override
public void switchChunk(String chunkType) {
if (chunk_ != null) {
DocDisplay docDisplay = target_.getDocDisplay();
Position start = chunk_.getPreamble();
Position end = chunk_.getEnd();
String chunkText = docDisplay.getTextForRange(Range.fromPoints(start, end));
JsArrayString chunkLines = StringUtil.split(chunkText, "\n");
if (chunkLines.length() > 0) {
String firstLine = chunkLines.get(0);
Position linedEnd = Position.create(start.getRow(), firstLine.length());
String newFirstLine = firstLine.replaceFirst("[, ]*engine='[a-zA-Z]+'", "");
newFirstLine = newFirstLine.replaceFirst("{[a-zA-Z]+", "{" + chunkType);
docDisplay.replaceRange(Range.fromPoints(start, linedEnd), newFirstLine);
target_.getNotebook().clearChunkOutput(chunk_);
}
}
}
use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position in project rstudio by rstudio.
the class TextEditingTargetPresentationHelper method navigateToSlide.
public static void navigateToSlide(final EditingTarget editor, int slideIndex) {
// scan for the specified slide
int currentSlide = 0;
Position navPos = null;
Position pos = Position.create(0, 0);
while ((pos = editor.search(pos, "^\\={3,}\\s*$")) != null) {
if (currentSlide++ == slideIndex) {
navPos = Position.create(pos.getRow() - 1, 0);
break;
}
pos = Position.create(pos.getRow() + 1, 0);
}
// navigate to the slide
if (navPos != null) {
final Position navPosAlias = navPos;
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
editor.navigateToPosition(SourcePosition.create(navPosAlias.getRow(), 0), false);
}
});
}
}
Aggregations