use of org.rstudio.studio.client.workbench.views.source.model.SourcePosition in project rstudio by rstudio.
the class ConnectionsPresenter method onPerformConnection.
@Override
public void onPerformConnection(PerformConnectionEvent event) {
String connectVia = event.getConnectVia();
String connectCode = event.getConnectCode();
if (connectVia.equals(ConnectionOptions.CONNECT_COPY_TO_CLIPBOARD)) {
DomUtils.copyCodeToClipboard(connectCode);
} else if (connectVia.equals(ConnectionOptions.CONNECT_R_CONSOLE)) {
eventBus_.fireEvent(new SendToConsoleEvent(connectCode, true));
display_.showConnectionProgress();
} else if (connectVia.equals(ConnectionOptions.CONNECT_NEW_R_SCRIPT) || connectVia.equals(ConnectionOptions.CONNECT_NEW_R_NOTEBOOK)) {
String type;
String code = connectCode;
SourcePosition cursorPosition = null;
if (connectVia.equals(ConnectionOptions.CONNECT_NEW_R_SCRIPT)) {
type = NewDocumentWithCodeEvent.R_SCRIPT;
code = code + "\n\n";
} else {
type = NewDocumentWithCodeEvent.R_NOTEBOOK;
int codeLength = code.split("\n").length;
code = "---\n" + "title: \"R Notebook\"\n" + "output: html_notebook\n" + "---\n" + "\n" + "```{r setup, include=FALSE}\n" + code + "\n" + "```\n" + "\n" + "```{r}\n" + "\n" + "```\n";
cursorPosition = SourcePosition.create(9 + codeLength, 0);
}
eventBus_.fireEvent(new NewDocumentWithCodeEvent(type, code, cursorPosition, true));
display_.showConnectionProgress();
}
}
use of org.rstudio.studio.client.workbench.views.source.model.SourcePosition in project rstudio by rstudio.
the class RCompletionManager method goToFunctionDefinition.
public void goToFunctionDefinition() {
// check for a file-local definition (intra-file navigation -- using
// the active scope tree)
AceEditor editor = (AceEditor) docDisplay_;
if (editor != null) {
TokenCursor cursor = editor.getSession().getMode().getRCodeModel().getTokenCursor();
if (cursor.moveToPosition(editor.getCursorPosition(), true)) {
// token (obstensibly a funciton name)
if (cursor.isLeftBracket())
cursor.moveToPreviousToken();
// navigate (as this isn't the 'full' function name)
if (cursor.moveToPreviousToken()) {
if (cursor.isExtractionOperator())
return;
cursor.moveToNextToken();
}
// if this is a string, try resolving that string as a file name
if (cursor.hasType("string")) {
String tokenValue = cursor.currentValue();
String path = tokenValue.substring(1, tokenValue.length() - 1);
FileSystemItem filePath = FileSystemItem.createFile(path);
// This will show a dialog error if no such file exists; this
// seems the most appropriate action in such a case.
fileTypeRegistry_.editFile(filePath);
}
String functionName = cursor.currentValue();
JsArray<ScopeFunction> scopes = editor.getAllFunctionScopes();
for (int i = 0; i < scopes.length(); i++) {
ScopeFunction scope = scopes.get(i);
if (scope.getFunctionName().equals(functionName)) {
navigableSourceEditor_.navigateToPosition(SourcePosition.create(scope.getPreamble().getRow(), scope.getPreamble().getColumn()), true);
return;
}
}
}
}
// intra-file navigation failed -- hit the server and find a definition
// in the project index
// determine current line and cursor position
InputEditorLineWithCursorPosition lineWithPos = InputEditorUtil.getLineWithCursorPosition(input_);
// delayed progress indicator
final GlobalProgressDelayer progress = new GlobalProgressDelayer(globalDisplay_, 1000, "Searching for function definition...");
server_.getObjectDefinition(lineWithPos.getLine(), lineWithPos.getPosition(), new ServerRequestCallback<ObjectDefinition>() {
@Override
public void onResponseReceived(ObjectDefinition def) {
// dismiss progress
progress.dismiss();
// if we got a hit
if (def.getObjectName() != null) {
// search locally if a function navigator was provided
if (navigableSourceEditor_ != null) {
// try to search for the function locally
SourcePosition position = navigableSourceEditor_.findFunctionPositionFromCursor(def.getObjectName());
if (position != null) {
navigableSourceEditor_.navigateToPosition(position, true);
// we're done
return;
}
}
// navigate to the file/loc
if (def.getObjectType() == FileFunctionDefinition.OBJECT_TYPE) {
FileFunctionDefinition fileDef = def.getObjectData().cast();
fileTypeRegistry_.editFile(fileDef.getFile(), fileDef.getPosition());
} else // search path definition
if (def.getObjectType() == SearchPathFunctionDefinition.OBJECT_TYPE) {
SearchPathFunctionDefinition searchDef = def.getObjectData().cast();
eventBus_.fireEvent(new CodeBrowserNavigationEvent(searchDef));
} else // finally, check to see if it's a data frame
if (def.getObjectType() == DataDefinition.OBJECT_TYPE) {
eventBus_.fireEvent(new SendToConsoleEvent("View(" + def.getObjectName() + ")", true, false));
}
}
}
@Override
public void onError(ServerError error) {
progress.dismiss();
globalDisplay_.showErrorMessage("Error Searching for Function", error.getUserMessage());
}
});
}
use of org.rstudio.studio.client.workbench.views.source.model.SourcePosition in project rstudio by rstudio.
the class Source method doOpenSourceFile.
private void doOpenSourceFile(final FileSystemItem file, final TextFileType fileType, final FilePosition position, final String pattern, final int navMethod, final boolean forceHighlightMode) {
// if the navigation should happen in another window, do that instead
NavigationResult navResult = windowManager_.navigateToFile(file, position, navMethod);
// we navigated externally, just skip this
if (navResult.getType() == NavigationResult.RESULT_NAVIGATED)
return;
// we're about to open in this window--if it's the main window, focus it
if (SourceWindowManager.isMainSourceWindow() && Desktop.isDesktop())
Desktop.getFrame().bringMainFrameToFront();
final boolean isDebugNavigation = navMethod == NavigationMethods.DEBUG_STEP || navMethod == NavigationMethods.DEBUG_END;
final CommandWithArg<EditingTarget> editingTargetAction = new CommandWithArg<EditingTarget>() {
@Override
public void execute(EditingTarget target) {
if (position != null) {
SourcePosition endPosition = null;
if (isDebugNavigation) {
DebugFilePosition filePos = (DebugFilePosition) position.cast();
endPosition = SourcePosition.create(filePos.getEndLine() - 1, filePos.getEndColumn() + 1);
if (Desktop.isDesktop() && navMethod != NavigationMethods.DEBUG_END)
Desktop.getFrame().bringMainFrameToFront();
}
navigate(target, SourcePosition.create(position.getLine() - 1, position.getColumn() - 1), endPosition);
} else if (pattern != null) {
Position pos = target.search(pattern);
if (pos != null) {
navigate(target, SourcePosition.create(pos.getRow(), 0), null);
}
}
}
private void navigate(final EditingTarget target, final SourcePosition srcPosition, final SourcePosition srcEndPosition) {
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
if (navMethod == NavigationMethods.DEBUG_STEP) {
target.highlightDebugLocation(srcPosition, srcEndPosition, true);
} else if (navMethod == NavigationMethods.DEBUG_END) {
target.endDebugHighlighting();
} else {
// force highlight mode if requested
if (forceHighlightMode)
target.forceLineHighlighting();
// now navigate to the new position
boolean highlight = navMethod == NavigationMethods.HIGHLIGHT_LINE && !uiPrefs_.highlightSelectedLine().getValue();
target.navigateToPosition(srcPosition, false, highlight);
}
}
});
}
};
if (navResult.getType() == NavigationResult.RESULT_RELOCATE) {
server_.getSourceDocument(navResult.getDocId(), new ServerRequestCallback<SourceDocument>() {
@Override
public void onResponseReceived(final SourceDocument doc) {
editingTargetAction.execute(addTab(doc, OPEN_REPLAY));
}
@Override
public void onError(ServerError error) {
globalDisplay_.showErrorMessage("Document Tab Move Failed", "Couldn't move the tab to this window: \n" + error.getMessage());
}
});
return;
}
final CommandWithArg<FileSystemItem> action = new CommandWithArg<FileSystemItem>() {
@Override
public void execute(FileSystemItem file) {
openFile(file, fileType, editingTargetAction);
}
};
// highlight in place.
if (isDebugNavigation) {
setPendingDebugSelection();
for (int i = 0; i < editors_.size(); i++) {
EditingTarget target = editors_.get(i);
String path = target.getPath();
if (path != null && path.equalsIgnoreCase(file.getPath())) {
// the file's open; just update its highlighting
if (navMethod == NavigationMethods.DEBUG_END) {
target.endDebugHighlighting();
} else {
view_.selectTab(i);
editingTargetAction.execute(target);
}
return;
}
}
// open a file just to turn off debug highlighting in the file!
if (navMethod == NavigationMethods.DEBUG_END)
return;
}
// Warning: event.getFile() can be null (e.g. new Sweave document)
if (file != null && file.getLength() < 0) {
// If the file has no size info, stat the file from the server. This
// is to prevent us from opening large files accidentally.
server_.stat(file.getPath(), new ServerRequestCallback<FileSystemItem>() {
@Override
public void onResponseReceived(FileSystemItem response) {
action.execute(response);
}
@Override
public void onError(ServerError error) {
// Couldn't stat the file? Proceed anyway. If the file doesn't
// exist, we'll let the downstream code be the one to show the
// error.
action.execute(file);
}
});
} else {
action.execute(file);
}
}
use of org.rstudio.studio.client.workbench.views.source.model.SourcePosition in project rstudio by rstudio.
the class AceEditor method scrollToBottom.
public void scrollToBottom() {
SourcePosition pos = SourcePosition.create(getCurrentLineCount() - 1, 0);
navigate(pos, false);
}
use of org.rstudio.studio.client.workbench.views.source.model.SourcePosition in project rstudio by rstudio.
the class AceEditor method fireRecordNavigationPosition.
private void fireRecordNavigationPosition(Position pos) {
SourcePosition srcPos = SourcePosition.create(pos.getRow(), pos.getColumn());
fireEvent(new RecordNavigationPositionEvent(srcPos));
}
Aggregations