Skip to content

Commit 37ec682

Browse files
committed
Add notes about how collect_sections() works
1 parent 6eac176 commit 37ec682

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

crates/ark/src/lsp/symbols.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,51 @@ fn collect_symbols(
214214
Ok(())
215215
}
216216

217+
/// This collects sections from comments like `# My section ----`. Like Markdown,
218+
/// sections may be nested depending on the number of `#` signs.
219+
///
220+
/// We collect sections in:
221+
/// - The top-level program list
222+
/// - In curly braces lists
223+
/// - In lists of call arguments.
224+
///
225+
/// Sections in calls are very useful for {targets} users, see
226+
/// <https://github.com/posit-dev/positron/issues/8402>.
227+
///
228+
/// Because our outline combines both markdown-like headers and syntax elements,
229+
/// we preserve syntax tree invariants. A section header is only allowed to
230+
/// close other headers at the current syntactic level. You can think of every
231+
/// level of blocks and calls as creating a new Markdown "document" that is
232+
/// nested under the current level. In practice this means that in this example:
233+
///
234+
/// ```r
235+
/// # top ----
236+
/// class <- R6::R6Class(
237+
/// 'class',
238+
/// public = list(
239+
/// initialize = function() {
240+
/// 'initialize'
241+
/// },
242+
/// # middle ----
243+
/// foo = function() {
244+
/// # inner ----
245+
/// 1
246+
/// },
247+
/// bar = function() {
248+
/// 2
249+
/// }
250+
/// )
251+
/// )
252+
/// ```
253+
///
254+
/// - `inner` is nested inside `middle` which is nested inside `top`
255+
/// - Other syntactic elements are included in the outline tree, e.g.
256+
/// `class` is nested within `top` and contains `middle`. The R6
257+
/// methods `foo` and `bar` are nested within `middle`.
258+
/// - In particular, `inner` does not close `middle` or `top`. If it
259+
/// were able to close another section across the syntax tree, this
260+
/// would create a confusing outline where e.g. the rest of R6 methods
261+
/// (`bar` in this case) would be pulled at top-level.
217262
fn collect_sections<F>(
218263
ctx: &mut CollectContext,
219264
node: &Node,

0 commit comments

Comments
 (0)