Skip to content

Commit 505561b

Browse files
authored
Add pastum.libraryDeclaration config option (#18)
2 parents 2166cc8 + 014f2ce commit 505561b

5 files changed

Lines changed: 36 additions & 22 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
# Change Log
2-
3-
All notable changes to the "pastum" extension will be documented in this file.
1+
# Changelog
42

53
<!-- Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. -->
64

5+
## Development version
6+
7+
### Added
8+
9+
- Markdown table support (thanks to @juarezr)
10+
- TSV and CSV table support (thanks to @juarezr)
11+
- `pastum.libraryDeclaration` configuration option, which allows the user to add library declaration to the pasted dataframe. (#18)
12+
- `pastum.airFormat` configuration option, which allows the user to add comment to skip air formatting in R. (#16)
13+
714
## [0.2.1] - 2024-11-02
815

916
### Added

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@
103103
"default": true,
104104
"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`."
105105
},
106+
"pastum.libraryDeclaration": {
107+
"type": "boolean",
108+
"default": true,
109+
"markdownDescription": "Add library declaration to the pasted dataframe. (i.e., `import pandas as pd`) before the pasted dataframe."
110+
},
106111
"pastum.airFormat": {
107112
"type": "boolean",
108113
"default": true,

src/paste-js.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ async function clipboardToJSDataFrame(framework = null) {
6262
*/
6363
function createJSDataFrame(tableData, framework) {
6464
const { headers, data, columnTypes } = tableData;
65+
const config = vscode.workspace.getConfiguration("pastum");
66+
const libraryDeclaration = config.get("libraryDeclaration");
6567
let code = "";
6668

6769
/**
@@ -100,7 +102,7 @@ function createJSDataFrame(tableData, framework) {
100102
});
101103
code += `};`;
102104
} else if (framework === "polars") {
103-
code = `import pl from "nodejs-polars";\n\n`;
105+
code = libraryDeclaration ? `import pl from "nodejs-polars";\n\n` : "";
104106
code += `const df = pl.DataFrame({\n`;
105107
headers.forEach((header, i) => {
106108
const values = data.map((row) => formatValue(row[i], i)).join(", ");
@@ -110,7 +112,7 @@ function createJSDataFrame(tableData, framework) {
110112
});
111113
code += `});`;
112114
} else if (framework === "arquero") {
113-
code = `import {table} from "arquero";\n\n`;
115+
code = libraryDeclaration ? `import {table} from "arquero";\n\n` : "";
114116
code += `const df = table({\n`;
115117
headers.forEach((header, i) => {
116118
const values = data.map((row) => formatValue(row[i], i)).join(", ");
@@ -120,7 +122,7 @@ function createJSDataFrame(tableData, framework) {
120122
});
121123
code += `});`;
122124
} else if (framework === "danfo") {
123-
code = `import * as dfd from "danfojs-node";\n\n`;
125+
code = libraryDeclaration ? `import * as dfd from "danfojs-node";\n\n` : "";
124126
code += `obj_data = {\n`;
125127
headers.forEach((header, i) => {
126128
const values = data.map((row) => formatValue(row[i], i)).join(", ");

src/paste-julia.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ function createJuliaDataFrame(tableData) {
6666
}
6767

6868
const { headers, data, columnTypes } = tableData;
69-
let code = `using DataFrames\n\n`;
69+
const config = vscode.workspace.getConfiguration("pastum");
70+
const libraryDeclaration = config.get("libraryDeclaration");
71+
let code = libraryDeclaration ? `using DataFrames\n\n` : "";
7072

7173
code += `DataFrame(\n`;
7274
headers.forEach((header, i) => {
7375
const values = data.map((row) => formatValue(row[i], i)).join(", ");
74-
code += ` :${header} => [${values}]${
75-
i < headers.length - 1 ? ",\n" : "\n"
76-
}`;
76+
code += ` :${header} => [${values}]${i < headers.length - 1 ? ",\n" : "\n"
77+
}`;
7778
});
7879
code += `)`;
7980

src/paste-python.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ async function clipboardToPyDataFrame(framework = null) {
6363
*/
6464
function createPyDataFrame(tableData, framework) {
6565
const { headers, data, columnTypes } = tableData;
66+
const config = vscode.workspace.getConfiguration("pastum");
67+
const libraryDeclaration = config.get("libraryDeclaration");
6668
let code = "";
6769

6870
/**
@@ -89,33 +91,30 @@ function createPyDataFrame(tableData, framework) {
8991

9092
// pandas
9193
if (framework === "pandas") {
92-
code = `import pandas as pd\n\n`;
94+
code = libraryDeclaration ? `import pandas as pd\n\n` : "";
9395
code += `pd.DataFrame({\n`;
9496
headers.forEach((header, i) => {
9597
const values = data.map((row) => formatValue(row[i], i)).join(", ");
96-
code += ` "${header}": [${values}]${
97-
i < headers.length - 1 ? ",\n" : "\n"
98-
}`;
98+
code += ` "${header}": [${values}]${i < headers.length - 1 ? ",\n" : "\n"
99+
}`;
99100
});
100101
code += `})`;
101102
} else if (framework === "datatable") {
102-
code = `import datatable as dt\n\n`;
103+
code = libraryDeclaration ? `import datatable as dt\n\n` : "";
103104
code += `dt.Frame({\n`;
104105
headers.forEach((header, i) => {
105106
const values = data.map((row) => formatValue(row[i], i)).join(", ");
106-
code += ` "${header}": [${values}]${
107-
i < headers.length - 1 ? ",\n" : "\n"
108-
}`;
107+
code += ` "${header}": [${values}]${i < headers.length - 1 ? ",\n" : "\n"
108+
}`;
109109
});
110110
code += `})`;
111111
} else if (framework === "polars") {
112-
code = `import polars as pl\n\n`;
112+
code = libraryDeclaration ? `import polars as pl\n\n` : "";
113113
code += `pl.DataFrame({\n`;
114114
headers.forEach((header, i) => {
115115
const values = data.map((row) => formatValue(row[i], i)).join(", ");
116-
code += ` "${header}": [${values}]${
117-
i < headers.length - 1 ? ",\n" : "\n"
118-
}`;
116+
code += ` "${header}": [${values}]${i < headers.length - 1 ? ",\n" : "\n"
117+
}`;
119118
});
120119
code += `})`;
121120
}

0 commit comments

Comments
 (0)