Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
hellocaoziyi committed Nov 25, 2022
1 parent 25acf62 commit 1ab68ec
Show file tree
Hide file tree
Showing 18 changed files with 493 additions and 10 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# smart-comments README
# Smart Comments

This is the README for your extension "smart-comments". After writing up a brief description, we recommend including the following sections.
This is an extension that helps with annotations, making the code easier to read.

`Ctrl`-`Shift`-`I`

## Features

Expand Down
Binary file modified images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed images/icon_large.png
Binary file not shown.
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 38 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
{
"name": "smart-comments",
"displayName": "Smart Comments",
"icon": "icon.png",
"description": "",
"description": "This extension helps with annotations, making the code easier to read",
"version": "0.0.1",
"icon": "images/icon.png",
"publisher": "Ziyi Cao",
"author": "Ziyi Cao",
"private": true,
"engines": {
"vscode": "^1.73.0"
},
"repository": {
"type": "git",
"url": "https://github.com/hellocaoziyi/smart-comments.git"
},
"categories": [
"Other"
"Formatters"
],
"activationEvents": [
"onCommand:smart-comments.helloWorld"
"onCommand:smart-comments.helloWorld",
"onStartupFinished",
"onCommand:extension.smartHeader"
],
"main": "./out/extension.js",
"contributes": {
"configuration": {
"type": "object",
"title": "Smart Comments Configuration",
"properties": {
"smartHeader": {
"type": "object",
"default": {},
"description": ""
}
}
},
"commands": [
{
"command": "smart-comments.helloWorld",
"title": "Hello World"
"command": "extension.smartHeader",
"title": "Smart Header"
}
],
"keybindings": [
{
"command": "extension.smartHeader",
"key": "ctrl+alt+i",
"mac": "ctrl+cmd+i",
"when": "editorTextFocus"
}
]
},
Expand All @@ -30,6 +58,10 @@
"lint": "eslint src --ext ts",
"test": "node ./out/test/runTest.js"
},
"dependencies": {
"moment": "^2.29.4",
"path": "^0.12.7"
},
"devDependencies": {
"@types/vscode": "^1.73.0",
"@types/glob": "^8.0.0",
Expand Down
9 changes: 9 additions & 0 deletions src/commands/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import CommandType from '../enums/commandType';
import ICommand from '../models/command';
import smartHeader from './smartHeader';

const commandList = new Map<CommandType, ICommand>([
[CommandType.smartHeader, {handler: smartHeader}],
]);

export default commandList;
28 changes: 28 additions & 0 deletions src/commands/smartHeader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import defaultConfig from '../config/default.config';
import ErrorType from '../enums/errorType';
import { generateHeaderTemplate } from '../utils/index';
import * as vscode from 'vscode';

const smartHeader = () => {
// Get configuration from user settings
const smartHeaderConfig = vscode.workspace.getConfiguration('smartHeader');
const format = (smartHeaderConfig && smartHeaderConfig.format) || defaultConfig.format;
const header = (smartHeaderConfig && smartHeaderConfig.header) || defaultConfig.header;

// Selected activated file the very first time the command is executed
const activeTextEditor = vscode.window.activeTextEditor;

activeTextEditor?.edit((editBuilder: vscode.TextEditorEdit) => {
try {
const filePath = activeTextEditor.document.fileName;
const headerTemplate = generateHeaderTemplate({format, header}, filePath);

// Insert Header
editBuilder.insert(new vscode.Position(0, 0), headerTemplate);
} catch (error) {
throw (new Error(ErrorType.InsertFailure));
}
});
};

export default smartHeader;
27 changes: 27 additions & 0 deletions src/config/default.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const defaultConfig = {
format: {
startWith: '/**',
middleWith: '*',
endWith: '*/',
headerPrefix: '@',
},
header: {
'Author': 'Your name',
'Create Time': {
type: 'createTime',
format: 'YYYY-MM-DD HH:mm:ss',
},
'Modified by': {
type: 'modifier',
value: 'Your name',
},
'Modified time': {
type: 'modifyTime',
format: 'YYYY-MM-DD HH:mm:ss',
},
'Company': 'Radar Resource Group, UESTC',
'Description': '',
},
};

export default defaultConfig;
5 changes: 5 additions & 0 deletions src/enums/commandType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum CommandType {
smartHeader = 'extension.smartHeader',
}

export default CommandType;
5 changes: 5 additions & 0 deletions src/enums/errorType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum ErrorType {
InsertFailure = 'Insert header failure.',
}

export default ErrorType;
12 changes: 12 additions & 0 deletions src/enums/fileType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
enum FileType {
Ejs = '.ejs',
Html = '.html',
Xml = '.xml',
Vue = '.vue',
Python = '.py',
Javascript = '.js',
Typescript = '.ts',
Matlab = '.m',
}

export default FileType;
7 changes: 7 additions & 0 deletions src/enums/itemType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enum ItemType {
CreateTime = 'createTime',
ModifyTime = 'modifyTime',
Modifier = 'modifier',
}

export default ItemType;
13 changes: 13 additions & 0 deletions src/events/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { workspace } from 'vscode';

import { onDidSaveTextDocument } from './workspace/index';

/**
* Handle events
*/
const handleEvents = () => {
// Handle workspace events
workspace.onDidSaveTextDocument(onDidSaveTextDocument);
};

export default handleEvents;
84 changes: 84 additions & 0 deletions src/events/workspace/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Position, Range, TextDocument, TextEditorEdit, window, workspace } from 'vscode';

import defaultConfig from '../../config/default.config';
import { checkLineStartsWith, getConfigOptionCount, getModify } from '../../utils/index';

// Last save time
let lastSaveTime = 0;

// Last save file name
let lastFileName = '';

/**
* File did save event handler
*/
export const onDidSaveTextDocument = (document: TextDocument) => {
if (document.fileName === lastFileName) {
const currentTime = new Date().getTime();
const timeInterval = currentTime - lastSaveTime;
if (timeInterval <= 5 * 1000) {
return;
}
}

lastSaveTime = new Date().getTime();
lastFileName = document.fileName;

// Get configuration from user settings
const smartHeaderConfig = workspace.getConfiguration('smartHeader');
const format = (smartHeaderConfig && smartHeaderConfig.format) || defaultConfig.format;
const header = (smartHeaderConfig && smartHeaderConfig.header) || defaultConfig.header;

// Selected activated file the very first time the command is executed
const activeTextEditor = window.activeTextEditor;

// Get modify entity
const modifyEntity = getModify({ format, header }, document.fileName);
const modifyTime = modifyEntity.modifyTime;
const modifier = modifyEntity.modifier;

const length = getConfigOptionCount(header);

let mofidyTimeRange = new Range(new Position(0, 0), new Position(0, 0));
let modifierRange = new Range(new Position(0, 0), new Position(0, 0));
const modifyTimeStartsWith = modifyEntity.modifyTime.matchPrefix//` ${format.middleWith} ${format.headerPrefix} ${modifyTime.key}:`;
const modifierStartsWith = modifyEntity.modifier.matchPrefix //` ${format.middleWith} ${format.headerPrefix} ${modifier.key}:`;
for (let index = 0; index < length; index++) {
// Get line text
const linetAt = document.lineAt(index);
const line = linetAt.text;

// tslint:disable-next-line:max-line-length
const isMofidyTimeLine = checkLineStartsWith(line, modifyTimeStartsWith);
if (isMofidyTimeLine) {
mofidyTimeRange = linetAt.range;
continue;
}

const isModifierLine = checkLineStartsWith(line, modifierStartsWith);
if (isModifierLine) {
modifierRange = linetAt.range;
}
}

const isUpdateModifyTime = !mofidyTimeRange.isEmpty && modifyTime.key && modifyTime.value;
const isUpdateModifier = !modifierRange.isEmpty && modifier.key && modifier.value;

if (!isUpdateModifyTime && !isUpdateModifier) {
return;
}

// Update header
activeTextEditor?.edit((editBuilder: TextEditorEdit) => {
if (isUpdateModifyTime) {
editBuilder.replace(mofidyTimeRange, modifyTime.value);
}

if (isUpdateModifier) {
editBuilder.replace(modifierRange, modifier.value);
}
});
setTimeout(() => {
document.save();
}, 200);
};
14 changes: 12 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';

import commandList from './commands/commands';
import handleEvents from './events/events';

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "smart-comments" is now active!');
console.log('"Smart Comments" is now active!');

// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
Expand All @@ -19,8 +22,15 @@ export function activate(context: vscode.ExtensionContext) {
vscode.window.showInformationMessage('Hello World from Smart Comments!');
});

for (const [key, value] of commandList) {
const command = vscode.commands.registerCommand(key, value.handler);
context.subscriptions.push(command);
}

handleEvents();

context.subscriptions.push(disposable);
}

// This method is called when your extension is deactivated
export function deactivate() {}
export function deactivate() {}
5 changes: 5 additions & 0 deletions src/models/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface ICommand {
handler: () => void;
}

export default ICommand;
Loading

0 comments on commit 1ab68ec

Please sign in to comment.