Skip to content

Instantly share code, notes, and snippets.

@InfectedIsm
Last active July 13, 2024 16:15
Show Gist options
  • Select an option

  • Save InfectedIsm/27ab29937181919b89589c62c0113db8 to your computer and use it in GitHub Desktop.

Select an option

Save InfectedIsm/27ab29937181919b89589c62c0113db8 to your computer and use it in GitHub Desktop.
Modification for copy-with-line-numbers extension for solidity reporting
  1. go to your vscode extensions folder and find the extension script file:
    .vscode\extensions\yassh.copy-with-line-numbers-0.0.4\out\src\copy-with-line-numbers.js
  2. replace with next file

Below the diff. I've replaced the use of copy-paste lib for clipboard as it does not work correctly with Vscode through Windows WSL. This change isn't mandatory.

--- C:\Users\.vscode\extensions\yassh.copy-with-line-numbers-0.0.4\out\src\copy-with-line-numbers - backup.js
+++ C:\Users\.vscode\extensions\yassh.copy-with-line-numbers-0.0.4\out\src\copy-with-line-numbers.js
@@ -3,7 +3,6 @@
 const vscode = require("vscode");
 const path = require("path");
 const os_1 = require("os");
-const copy_paste_1 = require("copy-paste");
 const leftPad = require("left-pad");
 const COPY_WITH_LINE_NUMBERS = 'extension.copyWithLineNumbers';
 const COPY_WITH_LINE_NUMBERS_WITHOUT_PATH = COPY_WITH_LINE_NUMBERS + '.withoutPath';
@@ -14,6 +13,7 @@
 const OPTION_WITH_RELATIVE_PATH = 'withRelativePath';
 const OPTION_WITH_FILE_NAME = 'withFileName';
 const MULTI_SELECTION_SEPARATOR = '---';
+
 function copyWithLineNumbers(option) {
     const editor = vscode.window.activeTextEditor;
     if (!editor) {
@@ -39,7 +39,8 @@
         default:
             break;
     }
-    let str = file ? `File: ${file}${os_1.EOL}` : '';
+    let str = "```solidity" + `${os_1.EOL}`
+    str += file ? `File: ${file}${os_1.EOL}` : '';
     const selections = [...editor.selections].sort((a, b) => a.start.line - b.start.line);
     const lastSelection = selections[selections.length - 1];
     const largestLineNumber = lastSelection.end.line + 1;
@@ -52,14 +53,16 @@
             const line = document.lineAt(n).text;
             str += `${number}: ${line}${os_1.EOL}`;
         }
+        str += "```"
+    });
 
-    });
-    copy_paste_1.copy(str, () => {
+    vscode.env.clipboard.writeText(str).then(() => {
         const showSuccessMessage = vscode.workspace.getConfiguration('copyWithLineNumbers').get('showSuccessMessage');
         if (showSuccessMessage)
             vscode.window.showInformationMessage('Copied!');
     });
 }
+
 exports.commands = {
     [COPY_WITH_LINE_NUMBERS_WITHOUT_PATH]: () => {
         copyWithLineNumbers();
@@ -74,6 +77,4 @@
         copyWithLineNumbers(OPTION_WITH_FILE_NAME);
     },
 };
\ No newline at end of file
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const vscode = require("vscode");
const path = require("path");
const os_1 = require("os");
const leftPad = require("left-pad");
const COPY_WITH_LINE_NUMBERS = 'extension.copyWithLineNumbers';
const COPY_WITH_LINE_NUMBERS_WITHOUT_PATH = COPY_WITH_LINE_NUMBERS + '.withoutPath';
const COPY_WITH_LINE_NUMBERS_WITH_FULL_PATH = COPY_WITH_LINE_NUMBERS + '.withFullPath';
const COPY_WITH_LINE_NUMBERS_WITH_RELATIVE_PATH = COPY_WITH_LINE_NUMBERS + '.withRelativePath';
const COPY_WITH_LINE_NUMBERS_WITH_FILE_NAME = COPY_WITH_LINE_NUMBERS + '.withFileName';
const OPTION_WITH_FULL_PATH = 'withFullPath';
const OPTION_WITH_RELATIVE_PATH = 'withRelativePath';
const OPTION_WITH_FILE_NAME = 'withFileName';
const MULTI_SELECTION_SEPARATOR = '---';
function copyWithLineNumbers(option) {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active');
return;
}
const document = editor.document;
const fullPath = document.fileName;
const relativePath = vscode.workspace.workspaceFolders ?
path.relative(vscode.workspace.workspaceFolders[0].uri.fsPath, fullPath) : fullPath;
const fileName = path.basename(fullPath);
let file = '';
switch (option) {
case OPTION_WITH_FULL_PATH:
file = fullPath;
break;
case OPTION_WITH_RELATIVE_PATH:
file = relativePath;
break;
case OPTION_WITH_FILE_NAME:
file = fileName;
break;
default:
break;
}
let str = "```solidity" + `${os_1.EOL}`
str += file ? `File: ${file}${os_1.EOL}` : '';
const selections = [...editor.selections].sort((a, b) => a.start.line - b.start.line);
const lastSelection = selections[selections.length - 1];
const largestLineNumber = lastSelection.end.line + 1;
const largestLineNumberLength = largestLineNumber.toString().length;
selections.forEach((selection, i) => {
if (i > 0)
str += `${MULTI_SELECTION_SEPARATOR}${os_1.EOL}`;
for (let n = selection.start.line; n <= selection.end.line; n += 1) {
const number = leftPad(n + 1, largestLineNumberLength, 0);
const line = document.lineAt(n).text;
str += `${number}: ${line}${os_1.EOL}`;
}
str += "```"
});
vscode.env.clipboard.writeText(str).then(() => {
const showSuccessMessage = vscode.workspace.getConfiguration('copyWithLineNumbers').get('showSuccessMessage');
if (showSuccessMessage)
vscode.window.showInformationMessage('Copied!');
});
}
exports.commands = {
[COPY_WITH_LINE_NUMBERS_WITHOUT_PATH]: () => {
copyWithLineNumbers();
},
[COPY_WITH_LINE_NUMBERS_WITH_FULL_PATH]: () => {
copyWithLineNumbers(OPTION_WITH_FULL_PATH);
},
[COPY_WITH_LINE_NUMBERS_WITH_RELATIVE_PATH]: () => {
copyWithLineNumbers(OPTION_WITH_RELATIVE_PATH);
},
[COPY_WITH_LINE_NUMBERS_WITH_FILE_NAME]: () => {
copyWithLineNumbers(OPTION_WITH_FILE_NAME);
},
};
//# sourceMappingURL=copy-with-line-numbers.js.map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment