MCPcopy Create free account
hub / github.com/REditorSupport/vscode-R / getChunks

Function getChunks

src/rmarkdown/chunks.ts:81–144  ·  view source on GitHub ↗
(document: vscode.TextDocument)

Source from the content-addressed store, hash-verified

79
80// Scan document and return chunk info (e.g. ID, chunk range) from all chunks
81export function getChunks(document: vscode.TextDocument): RMarkdownChunk[] {
82 const lines = document.getText().split(/\r?\n/);
83 const chunks: RMarkdownChunk[] = [];
84
85 let line = 0;
86 let chunkId = 0; // One-based index
87 let chunkStartLine: number | undefined = undefined;
88 let chunkEndLine: number | undefined = undefined;
89 let codeEndLine: number | undefined = undefined;
90 let chunkLanguage: string | undefined = undefined;
91 let chunkOptions: string | undefined = undefined;
92 let chunkEval: boolean | undefined = undefined;
93 const isRDoc = isRDocument(document);
94
95 while (line < lines.length) {
96 if (chunkStartLine === undefined) {
97 if (isChunkStartLine(lines[line], isRDoc)) {
98 chunkId++;
99 chunkStartLine = line;
100 chunkLanguage = getChunkLanguage(lines[line], isRDoc);
101 chunkOptions = getChunkOptions(lines[line], isRDoc);
102 chunkEval = getChunkEval(chunkOptions);
103 }
104 } else {
105 // Second condition is for the last chunk in an .R file
106 const isRDocAndFinalLine = (isRDoc && line === lines.length - 1);
107 if (isChunkEndLine(lines[line], isRDoc) || isRDocAndFinalLine) {
108 chunkEndLine = line;
109 codeEndLine = line - 1;
110
111 // isChunkEndLine looks for `# %%` in `.R` files, so if found, then need to go back one line to mark end of code chunk.
112 if (isRDoc && !isRDocAndFinalLine) {
113 chunkEndLine = chunkEndLine - 1;
114 codeEndLine = chunkEndLine;
115 line = line - 1;
116 }
117
118 const chunkRange = new vscode.Range(
119 new vscode.Position(chunkStartLine, 0),
120 new vscode.Position(line, lines[line].length)
121 );
122 const codeRange = new vscode.Range(
123 new vscode.Position(chunkStartLine + 1, 0),
124 new vscode.Position(codeEndLine, lines[codeEndLine].length)
125 );
126
127 chunks.push({
128 id: chunkId, // One-based index
129 startLine: chunkStartLine,
130 endLine: chunkEndLine,
131 language: chunkLanguage,
132 options: chunkOptions,
133 eval: chunkEval,
134 chunkRange: chunkRange,
135 codeRange: codeRange
136 });
137
138 chunkStartLine = undefined;

Callers 9

provideHoverMethod · 0.90
provideHoverMethod · 0.90
provideCodeLensesMethod · 0.90
provideFoldingRangesMethod · 0.90
_getChunksFunction · 0.85

Calls 6

isRDocumentFunction · 0.85
isChunkStartLineFunction · 0.85
getChunkLanguageFunction · 0.85
getChunkOptionsFunction · 0.85
getChunkEvalFunction · 0.85
isChunkEndLineFunction · 0.85

Tested by

no test coverage detected