From db8be62e114d6b7a47eea81bc6a0f61b15697779 Mon Sep 17 00:00:00 2001 From: atsyplenkov Date: Fri, 12 Sep 2025 10:48:13 +1200 Subject: [PATCH 1/2] feat: skip library declaration (closes #18) --- package.json | 5 +++++ src/paste-js.js | 8 +++++--- src/paste-julia.js | 9 +++++---- src/paste-python.js | 23 +++++++++++------------ 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 058e733..cb656a5 100644 --- a/package.json +++ b/package.json @@ -103,6 +103,11 @@ "default": true, "markdownDescription": "Show the `Pastum: paste as default dataframe` command in the editor context menu *(i.e., right-click menu)*. It will only appear in R, Python, and Julia editors. The dataframe will be pasted according to the specified `pastum.defaultDataframeR` and `pastum.defaultDataframePy`." }, + "pastum.libraryDeclaration": { + "type": "boolean", + "default": true, + "markdownDescription": "Add library declaration to the pasted dataframe. (i.e., `import pandas as pd`) before the pasted dataframe." + }, "pastum.airFormat": { "type": "boolean", "default": true, diff --git a/src/paste-js.js b/src/paste-js.js index 776d11a..7e94c7b 100644 --- a/src/paste-js.js +++ b/src/paste-js.js @@ -62,6 +62,8 @@ async function clipboardToJSDataFrame(framework = null) { */ function createJSDataFrame(tableData, framework) { const { headers, data, columnTypes } = tableData; + const config = vscode.workspace.getConfiguration("pastum"); + const libraryDeclaration = config.get("libraryDeclaration"); let code = ""; /** @@ -100,7 +102,7 @@ function createJSDataFrame(tableData, framework) { }); code += `};`; } else if (framework === "polars") { - code = `import pl from "nodejs-polars";\n\n`; + code = libraryDeclaration ? `import pl from "nodejs-polars";\n\n` : ""; code += `const df = pl.DataFrame({\n`; headers.forEach((header, i) => { const values = data.map((row) => formatValue(row[i], i)).join(", "); @@ -110,7 +112,7 @@ function createJSDataFrame(tableData, framework) { }); code += `});`; } else if (framework === "arquero") { - code = `import {table} from "arquero";\n\n`; + code = libraryDeclaration ? `import {table} from "arquero";\n\n` : ""; code += `const df = table({\n`; headers.forEach((header, i) => { const values = data.map((row) => formatValue(row[i], i)).join(", "); @@ -120,7 +122,7 @@ function createJSDataFrame(tableData, framework) { }); code += `});`; } else if (framework === "danfo") { - code = `import * as dfd from "danfojs-node";\n\n`; + code = libraryDeclaration ? `import * as dfd from "danfojs-node";\n\n` : ""; code += `obj_data = {\n`; headers.forEach((header, i) => { const values = data.map((row) => formatValue(row[i], i)).join(", "); diff --git a/src/paste-julia.js b/src/paste-julia.js index 0a7e8e4..f563f0a 100644 --- a/src/paste-julia.js +++ b/src/paste-julia.js @@ -66,14 +66,15 @@ function createJuliaDataFrame(tableData) { } const { headers, data, columnTypes } = tableData; - let code = `using DataFrames\n\n`; + const config = vscode.workspace.getConfiguration("pastum"); + const libraryDeclaration = config.get("libraryDeclaration"); + let code = libraryDeclaration ? `using DataFrames\n\n` : ""; code += `DataFrame(\n`; headers.forEach((header, i) => { const values = data.map((row) => formatValue(row[i], i)).join(", "); - code += ` :${header} => [${values}]${ - i < headers.length - 1 ? ",\n" : "\n" - }`; + code += ` :${header} => [${values}]${i < headers.length - 1 ? ",\n" : "\n" + }`; }); code += `)`; diff --git a/src/paste-python.js b/src/paste-python.js index 35dcf0b..d915e73 100644 --- a/src/paste-python.js +++ b/src/paste-python.js @@ -63,6 +63,8 @@ async function clipboardToPyDataFrame(framework = null) { */ function createPyDataFrame(tableData, framework) { const { headers, data, columnTypes } = tableData; + const config = vscode.workspace.getConfiguration("pastum"); + const libraryDeclaration = config.get("libraryDeclaration"); let code = ""; /** @@ -89,33 +91,30 @@ function createPyDataFrame(tableData, framework) { // pandas if (framework === "pandas") { - code = `import pandas as pd\n\n`; + code = libraryDeclaration ? `import pandas as pd\n\n` : ""; code += `pd.DataFrame({\n`; headers.forEach((header, i) => { const values = data.map((row) => formatValue(row[i], i)).join(", "); - code += ` "${header}": [${values}]${ - i < headers.length - 1 ? ",\n" : "\n" - }`; + code += ` "${header}": [${values}]${i < headers.length - 1 ? ",\n" : "\n" + }`; }); code += `})`; } else if (framework === "datatable") { - code = `import datatable as dt\n\n`; + code = libraryDeclaration ? `import datatable as dt\n\n` : ""; code += `dt.Frame({\n`; headers.forEach((header, i) => { const values = data.map((row) => formatValue(row[i], i)).join(", "); - code += ` "${header}": [${values}]${ - i < headers.length - 1 ? ",\n" : "\n" - }`; + code += ` "${header}": [${values}]${i < headers.length - 1 ? ",\n" : "\n" + }`; }); code += `})`; } else if (framework === "polars") { - code = `import polars as pl\n\n`; + code = libraryDeclaration ? `import polars as pl\n\n` : ""; code += `pl.DataFrame({\n`; headers.forEach((header, i) => { const values = data.map((row) => formatValue(row[i], i)).join(", "); - code += ` "${header}": [${values}]${ - i < headers.length - 1 ? ",\n" : "\n" - }`; + code += ` "${header}": [${values}]${i < headers.length - 1 ? ",\n" : "\n" + }`; }); code += `})`; } From 014f2cef36546eef1b48b19a66e49cf42f36e7e9 Mon Sep 17 00:00:00 2001 From: atsyplenkov Date: Fri, 12 Sep 2025 10:54:49 +1200 Subject: [PATCH 2/2] update changelog --- CHANGELOG.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79ae65f..5c1cb64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,16 @@ -# Change Log - -All notable changes to the "pastum" extension will be documented in this file. +# Changelog +## Development version + +### Added + +- Markdown table support (thanks to @juarezr) +- TSV and CSV table support (thanks to @juarezr) +- `pastum.libraryDeclaration` configuration option, which allows the user to add library declaration to the pasted dataframe. (#18) +- `pastum.airFormat` configuration option, which allows the user to add comment to skip air formatting in R. (#16) + ## [0.2.1] - 2024-11-02 ### Added