-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25acf62
commit 1ab68ec
Showing
18 changed files
with
493 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
enum CommandType { | ||
smartHeader = 'extension.smartHeader', | ||
} | ||
|
||
export default CommandType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
enum ErrorType { | ||
InsertFailure = 'Insert header failure.', | ||
} | ||
|
||
export default ErrorType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
interface ICommand { | ||
handler: () => void; | ||
} | ||
|
||
export default ICommand; |
Oops, something went wrong.