|
1 | 1 | import { EOL } from 'os'; |
2 | | -import { Range, TextEditor, window } from 'vscode'; |
| 2 | +import { Position, Range, TextDocument, TextEditor, window } from 'vscode'; |
3 | 3 | import * as Constants from '../common/constants'; |
4 | 4 | import { fromString as ParseReqMetaKey, RequestMetadata } from '../models/requestMetadata'; |
5 | 5 | import { SelectedRequest } from '../models/SelectedRequest'; |
@@ -28,7 +28,19 @@ export class Selector { |
28 | 28 | let selectedText: string | null; |
29 | 29 | if (editor.selection.isEmpty || range) { |
30 | 30 | const activeLine = range?.start.line ?? editor.selection.active.line; |
31 | | - selectedText = this.getDelimitedText(editor.document.getText(), activeLine); |
| 31 | + if (editor.document.languageId === 'markdown') { |
| 32 | + selectedText = null; |
| 33 | + |
| 34 | + for (const r of Selector.getMarkdownRestSnippets(editor.document)) { |
| 35 | + const snippetRange = new Range(r.start.line + 1, 0, r.end.line, 0); |
| 36 | + if (snippetRange.contains(new Position(activeLine, 0))) { |
| 37 | + selectedText = editor.document.getText(snippetRange); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + } else { |
| 42 | + selectedText = this.getDelimitedText(editor.document.getText(), activeLine); |
| 43 | + } |
32 | 44 | } else { |
33 | 45 | selectedText = editor.document.getText(editor.selection); |
34 | 46 | } |
@@ -231,6 +243,31 @@ export class Selector { |
231 | 243 | .map(([index, ]) => +index); |
232 | 244 | } |
233 | 245 |
|
| 246 | + public static* getMarkdownRestSnippets(document: TextDocument): Generator<Range> { |
| 247 | + const snippetStartRegx = new RegExp('^\`\`\`(' + ['http', 'rest'].join('|') + ')$'); |
| 248 | + const snippetEndRegx = /^\`\`\`$/; |
| 249 | + |
| 250 | + let snippetStart: number | null = null; |
| 251 | + for (let i = 0; i < document.lineCount; i++) { |
| 252 | + const lineText = document.lineAt(i).text; |
| 253 | + |
| 254 | + const matchEnd = lineText.match(snippetEndRegx); |
| 255 | + if (snippetStart !== null && matchEnd) { |
| 256 | + const snippetEnd = i; |
| 257 | + |
| 258 | + const range = new Range(snippetStart, 0, snippetEnd, 0); |
| 259 | + yield range; |
| 260 | + |
| 261 | + snippetStart = null; |
| 262 | + } else { |
| 263 | + const matchStart = lineText.match(snippetStartRegx); |
| 264 | + if (matchStart) { |
| 265 | + snippetStart = i; |
| 266 | + } |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + |
234 | 271 | private static handlePromptMetadata(metadatas: Map<RequestMetadata, string | undefined> , text: string) { |
235 | 272 | const promptVarDef = this.getPrompVariableDefinition(text); |
236 | 273 | if (promptVarDef) { |
|
0 commit comments