From 9eb422f0623885ac70eb7a0b8e5141ead6669dd2 Mon Sep 17 00:00:00 2001 From: Gabriel Guedes Date: Thu, 16 Mar 2023 15:47:05 -0300 Subject: [PATCH] renderCursor setting --- package.json | 15 ++++++++++-- src/configloader.ts | 50 ++++++++++++++++++++++++++++++++++++++-- src/providers/Preview.ts | 15 +++++++++++- 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 059f5726..a52bf54e 100644 --- a/package.json +++ b/package.json @@ -144,9 +144,20 @@ }, "fountain.general.previewTexture": { "type": "boolean", - "default": true, + "default": false, "description": "Noise texture on the live preview (absent from PDF)" - } + }, + "fountain.renderCursor": { + "type": "boolean", + "default": true, + "description": "Render only a certain number of lines around the cursor" + }, + "fountain.numLines": { + "type": "integer", + "default": 500, + "description": "Number of lines to render around the cursor" + } + } }, { diff --git a/src/configloader.ts b/src/configloader.ts index 3b06d664..4f44eaa0 100755 --- a/src/configloader.ts +++ b/src/configloader.ts @@ -120,6 +120,52 @@ export var getFountainConfig = function(docuri:vscode.Uri):FountainConfig{ synchronized_markup_and_preview: generalConfig.synchronizedMarkupAndPreview, preview_theme: generalConfig.previewTheme, preview_texture: generalConfig.previewTexture, - parenthetical_newline_helper: generalConfig.parentheticalNewLineHelper + parenthetical_newline_helper: generalConfig.parentheticalNewLineHelper, + numLines: generalConfig.get("numLines", 500), // new config property } -} \ No newline at end of file +} + +// Render preview with cursor +function renderCursor(activateCursor: boolean, numLines: number): void { + const editor = vscode.window.activeTextEditor; + if (!editor) { + return; + } + + if (!activateCursor) { + renderPreview(); + return; + } + + const cursorPos = getCursorPosition(); + const startLine = Math.max(0, cursorPos.line - Math.floor(numLines / 2)); + const endLine = Math.min(editor.document.lineCount - 1, startLine + numLines - 1); + const visibleRange = new vscode.Range(startLine, 0, endLine, editor.document.lineAt(endLine).text.length); + editor.revealRange(visibleRange, vscode.TextEditorRevealType.InCenter); + renderPreview(); + } + + // Get the cursor position + function getCursorPosition(): vscode.Position { + const editor = vscode.window.activeTextEditor; + if (!editor) { + return new vscode.Position(0, 0); + } + + return editor.selection.active; + } + + // Render preview + function renderPreview(): void { + // Your code to render the preview here + } + + // Activate the extension + export function activate(context: vscode.ExtensionContext) { + const config = vscode.workspace.getConfiguration('fountain'); + const activateCursor = config.get('renderCursor', true); + const numLines = config.get('numLines', 500); + + // Call renderCursor with the settings + renderCursor(activateCursor, numLines); + } \ No newline at end of file diff --git a/src/providers/Preview.ts b/src/providers/Preview.ts index 05e455be..ff021d0f 100755 --- a/src/providers/Preview.ts +++ b/src/providers/Preview.ts @@ -212,4 +212,17 @@ export class FountainPreviewSerializer implements vscode.WebviewPanelSerializer //webviewPanel.webview.postMessage({ command: 'updateTitle', content: state.title_html }); //webviewPanel.webview.postMessage({ command: 'updateScript', content: state.screenplay_html }); } - } \ No newline at end of file + } +function renderCursor(activateCursor: boolean, numLines: number): void { + if (!activateCursor) { + renderPreview(); + return; + } + + const cursorPos = getCursorPosition(); + const startLine = Math.max(0, cursorPos.line - Math.floor(numLines / 2)); + const endLine = Math.min(editor.lineCount - 1, startLine + numLines - 1); + const visibleRange = new vscode.Range(startLine, 0, endLine, editor.document.lineAt(endLine).text.length); + editor.revealRange(visibleRange, vscode.TextEditorRevealType.InCenter); + renderPreview(); +}