Skip to content

Commit

Permalink
Added blockComment function.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennykorsukewitz committed Aug 29, 2023
1 parent c0d1e6d commit 25c9ae1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ This Function provides a searchable list of folders (Workspaces) that can be rem

## QuoteWithMarker

### Leading zeros

Added leading zeros to month and day to always get the same date format.

### blockComment

Some languages do not have line comments, such as CSS.
But, they have the possibility to comment out a code block (blockComment).
Now the QuoteWithMarker can be used in these languages as well.

## [1.1.6]

### QuoteWithMarker
Expand Down
8 changes: 8 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ This Function provides a searchable list of folders (Workspaces) that can be rem

## QuoteWithMarker

### Leading zeros

Added leading zeros to month and day to always get the same date format.

### blockComment

Some languages do not have line comments, such as CSS.
But, they have the possibility to comment out a code block (blockComment).
Now the QuoteWithMarker can be used in these languages as well.
67 changes: 49 additions & 18 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,21 +452,25 @@ function initQuoteWithMarker(context) {
let text = activeEditor.document.getText(selection) || '';
let config = vscode.workspace.getConfiguration('znuny').get('quoteWithMarker');

let quoteChar,
let quoteCharStart = '',
quoteCharEnd = '',
quoteCharStartOrigin = '',
quoteCharBlockStart = '',
isBlockComment = 0,
codeMarkerReplace,
codeMarker = config.codeMarker || 'Znuny',
lineComment = config.lineComment || {},
languageId = activeEditor.document.languageId;

let currentTime = new Date();

// returns the month (from 0 to 11)
// returns the month (from 0 to 11).
let month = currentTime.getMonth() + 1;

// returns the day of the month (from 1 to 31);
// returns the day of the month (from 1 to 31).
let day = currentTime.getDate();

// returns the year (four digits)
// returns the year (four digits).
let year = currentTime.getFullYear();

if (month.toString().length <= 1){
Expand All @@ -480,14 +484,14 @@ function initQuoteWithMarker(context) {
codeMarker = codeMarker.replace(/\${month}/g, month);
codeMarker = codeMarker.replace(/\${day}/g, day);

// Get quoteChar from config
if (lineComment && lineComment[languageId] && lineComment[languageId].length) {
quoteChar = lineComment[languageId];
// Get quoteCharStart from config.
if (lineComment[languageId] && lineComment[languageId].length){
quoteCharStart = lineComment[languageId];
}

// If no quoteChar is set, try to use the default value of lineComment of the current language config
if (quoteChar.length === 0 && languageId) {
let extensions = vscode.extensions.all;
// If no quoteCharStart is set, try to use the default value of lineComment of the current language config.
if (quoteCharStart.length === 0 && languageId || quoteCharStart === 'undefined'){
let extensions = vscode.extensions.all;
let languagesData = extensions.filter((extension) => extension.packageJSON.name === languageId);

let languageExtensionPath = languagesData[0].extensionPath;
Expand All @@ -498,25 +502,52 @@ function initQuoteWithMarker(context) {

try {
const config = JSON.parse(content);
quoteChar = config.comments.lineComment;
if (config.comments.lineComment){
quoteCharStart = config.comments.lineComment;
}
else if (config.comments.blockComment){
isBlockComment = 1;
quoteCharStart = config.comments.blockComment[0];
quoteCharEnd = ' ' + config.comments.blockComment[1];
quoteCharBlockStart = ' ' + config.comments.blockComment[0].substring(1);
}

} catch (error) {
console.log(error);
}
}

if (!quoteChar) return;
if (!quoteCharStart) return;

let lineLength = 0;
text.split(/\r?\n/).forEach(line => {
if (line.toString().length > lineLength){
lineLength = line.toString().length;
}
})

codeMarkerReplace = `${quoteChar} ---\n`;
codeMarkerReplace += `${quoteChar} ${codeMarker}\n`;
codeMarkerReplace += `${quoteChar} ---\n`;
codeMarkerReplace = `${quoteCharStart} ---${quoteCharEnd}\n`;
codeMarkerReplace += `${quoteCharStart} ${codeMarker}${quoteCharEnd}\n`;
codeMarkerReplace += `${quoteCharStart} ---${quoteCharEnd}\n`;

// Add QuoteChar to every single line.
if (isBlockComment) {
codeMarkerReplace += `${quoteCharStart}\n`;
quoteCharStartOrigin = quoteCharStart;
quoteCharStart = quoteCharBlockStart;
}

// Add QuoteCharStart to every single line.
text.split(/\r?\n/).forEach(line => {
codeMarkerReplace += `${quoteChar} ${line}\n`;
codeMarkerReplace += `${quoteCharStart} ${line}\n`;
})

if (isBlockComment) {
codeMarkerReplace += `${quoteCharEnd}\n`;
quoteCharStart = quoteCharStartOrigin;
}

codeMarkerReplace += `\n${text}`;
codeMarkerReplace += `\n\n${quoteChar} ---\n`;
codeMarkerReplace += `\n\n${quoteCharStart} ---${quoteCharEnd}\n`;
text.replace(text, codeMarkerReplace);

// Replace the selection in the editor with CodeMarker.
Expand Down

0 comments on commit 25c9ae1

Please sign in to comment.