1 /*
2  * Scope.java
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 package org.rstudio.studio.client.workbench.views.source.editors.text;
16 
17 import com.google.gwt.core.client.JavaScriptObject;
18 import com.google.gwt.core.client.JsArray;
19 import org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position;
20 
21 public class Scope extends JavaScriptObject
22 {
Scope()23    protected Scope()
24    {}
25 
createRScopeNode(String label, Position start, Position end, int scopeType)26    public static final native Scope createRScopeNode(String label,
27                                                      Position start,
28                                                      Position end,
29                                                      int scopeType)
30    /*-{
31       var ScopeNode = $wnd.require("mode/r_scope_tree").ScopeNode;
32       var node = new ScopeNode(label, start, start, scopeType);
33       node.end = end;
34       return node;
35    }-*/;
36 
getLabel()37    public native final String getLabel() /*-{
38       return this.label;
39    }-*/;
40 
isTopLevel()41    public native final boolean isTopLevel() /*-{
42       return this.isRoot();
43    }-*/;
44 
isBrace()45    public native final boolean isBrace() /*-{
46       return this.isBrace();
47    }-*/;
48 
isChunk()49    public native final boolean isChunk() /*-{
50       return this.isChunk();
51    }-*/;
52 
isSection()53    public native final boolean isSection() /*-{
54       return this.isSection();
55    }-*/;
56 
isFunction()57    public native final boolean isFunction() /*-{
58       return this.isFunction();
59    }-*/;
60 
getParentScope()61    public native final Scope getParentScope() /*-{
62       return this.parentScope;
63    }-*/;
64 
65    /**
66     * For named functions, the preamble points to the beginning of the function
67     * declaration, including function name. For chunks, it points to the
68     * beginning of the chunk itself. For other scopes, it just points to the
69     * opening brace (same as getBodyStart).
70     */
getPreamble()71    public native final Position getPreamble() /*-{
72       return this.preamble;
73    }-*/;
74 
75    /**
76     * Points to the start of the body of the scope. Note that for named
77     * functions, chunks, and sections, this is different than the preamble.
78     */
getBodyStart()79    public native final Position getBodyStart() /*-{
80       return this.start;
81    }-*/;
82 
83    /**
84     * Points to the part of a scope where a fold would begin.
85     */
getFoldStart()86    public final Position getFoldStart()
87    {
88       if (isFunction())
89          return getBodyStart();
90       else
91          return getPreamble();
92 
93    }
94 
getEnd()95    public native final Position getEnd() /*-{
96       return this.end;
97    }-*/;
98 
getChildren()99    public native final JsArray<Scope> getChildren() /*-{
100       return this.$children;
101    }-*/;
102 
getChunkLabel()103    public native final String getChunkLabel() /*-{
104       return this.chunkLabel;
105    }-*/;
106 
isClass()107    public native final boolean isClass() /*-{
108       return typeof this.isClass !== "undefined" && this.isClass();
109    }-*/;
110 
isNamespace()111    public native final boolean isNamespace() /*-{
112       return typeof this.isNamespace !== "undefined" && this.isNamespace();
113    }-*/;
114 
isLambda()115    public native final boolean isLambda() /*-{
116       return typeof this.isLambda !== "undefined" && this.isLambda();
117    }-*/;
118 
isAnon()119    public native final boolean isAnon() /*-{
120       return typeof this.isAnon !== "undefined" && this.isAnon();
121    }-*/;
122 
isMarkdownHeader()123    public native final boolean isMarkdownHeader() /*-{
124       return this.attributes && this.attributes.isMarkdown === true;
125    }-*/;
126 
isYaml()127    public native final boolean isYaml() /*-{
128       return this.attributes && this.attributes.isYaml === true;
129    }-*/;
130 
getDepth()131    public native final int getDepth() /*-{
132       return this.attributes && this.attributes.depth || 0;
133    }-*/;
134 
135    public static final int SCOPE_TYPE_ROOT    = 1;
136    public static final int SCOPE_TYPE_BRACE   = 2;
137    public static final int SCOPE_TYPE_CHUNK   = 3;
138    public static final int SCOPE_TYPE_SECTION = 4;
139 }
140