-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨config active without restart vscode
- Loading branch information
Showing
5 changed files
with
51 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
* @Author : Robert Huang<[email protected]> * | ||
* @CreatedDate : 2023-02-04 10:01:21 * | ||
* @LastEditors : Robert Huang<[email protected]> * | ||
* @LastEditDate : 2023-02-05 23:40:46 * | ||
* @LastEditDate : 2023-02-07 15:23:41 * | ||
* @FilePath : auto-header-plus/src/extension.ts * | ||
* @CopyRight : MerBleueAviation * | ||
*****************************************************************************/ | ||
|
@@ -14,14 +14,12 @@ import packageJson from '../package.json' | |
import { handleNew, handleSave } from './handle' | ||
import { addHeader } from './header' | ||
import { Logger } from './logger' | ||
|
||
const config = vscode.workspace.getConfiguration(packageJson.name) | ||
//console.debug(config) | ||
import { config } from './utils' | ||
|
||
const logger = new Logger() | ||
logger.setOutputLevel(config.get('logLevel', 'INFO')) | ||
logger.setOutputLevel(config().get('logLevel', 'INFO')) | ||
|
||
export { config, logger } | ||
export { logger } | ||
|
||
// This method is called when your extension is activated | ||
// Your extension is activated the very first time the command is executed | ||
|
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 |
---|---|---|
|
@@ -2,19 +2,20 @@ | |
* @Author : Robert Huang<[email protected]> * | ||
* @CreatedDate : 2023-02-03 23:59:50 * | ||
* @LastEditors : Robert Huang<[email protected]> * | ||
* @LastEditDate : 2023-02-04 10:36:10 * | ||
* @LastEditDate : 2023-02-07 15:24:40 * | ||
* @FilePath : auto-header-plus/src/handle.ts * | ||
* @CopyRight : MerBleueAviation * | ||
*****************************************************************************/ | ||
|
||
import * as vscode from 'vscode' | ||
import { config, logger } from './extension' | ||
import { logger } from './extension' | ||
import { addHeader } from './header' | ||
import { config } from './utils' | ||
const t = vscode.l10n.t | ||
|
||
const handleNew = () => { | ||
logger.debug('New file') | ||
if (config.get('enableAutoAddOnNew')) { | ||
if (config().get('enableAutoAddOnNew')) { | ||
addHeader() | ||
} else { | ||
logger.info(t('Auto add on new is disabled')) | ||
|
@@ -23,7 +24,7 @@ const handleNew = () => { | |
|
||
const handleSave = (e: vscode.TextDocumentWillSaveEvent) => { | ||
logger.debug('Save file') | ||
if (config.get('enableAutoAddOnSave')) { | ||
if (config().get('enableAutoAddOnSave')) { | ||
e.document.isDirty ? addHeader() : logger.info(t('File not changed')) | ||
} else { | ||
logger.info(t('Auto add on save is disabled')) | ||
|
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 |
---|---|---|
|
@@ -2,15 +2,15 @@ | |
* @Author : Robert Huang<[email protected]> * | ||
* @CreatedDate : 2023-02-04 00:03:21 * | ||
* @LastEditors : Robert Huang<[email protected]> * | ||
* @LastEditDate : 2023-02-06 20:11:05 * | ||
* @LastEditDate : 2023-02-07 15:27:44 * | ||
* @FilePath : auto-header-plus/src/header.ts * | ||
* @CopyRight : MerBleueAviation * | ||
*****************************************************************************/ | ||
|
||
import path from 'path' | ||
import * as vscode from 'vscode' | ||
import { config, logger } from './extension' | ||
import { getApplyStyle, getDateValue, getFinalStringContainsCmd, getPathValue, splitString } from './utils' | ||
import { logger } from './extension' | ||
import { config, getApplyStyle, getDateValue, getFinalStringContainsCmd, getPathValue, splitString } from './utils' | ||
|
||
const t = vscode.l10n.t | ||
const SPECVALUE = ['MODIFIEDDATE', 'CREATEDDATE', 'FULLPATH', 'RELATIVEPATH', 'SHORTNAMEPATH'] | ||
|
@@ -235,7 +235,7 @@ const genNewHeader = ( | |
oldCommentElementsValues: ahp.CommentElementsValues | ||
): string => { | ||
let headerText = '' | ||
const allCreateDateDiff = config.get('allCreateDateDiff', true) | ||
const allCreateDateDiff = config().get('allCreateDateDiff', true) | ||
const eolText = doc.eol === vscode.EndOfLine.LF ? '\r' : '\r\n' | ||
|
||
// firstLine | ||
|
@@ -288,7 +288,7 @@ const genNewHeader = ( | |
} | ||
|
||
// additional comment | ||
const additionalComment = config.get('additionalComment', '') | ||
const additionalComment = config().get('additionalComment', '') | ||
if (additionalComment.length > 0) { | ||
const additionalCommentText = getFinalStringContainsCmd(additionalComment) || '' | ||
splitString(additionalCommentText, style.lineWidth).forEach((line) => { | ||
|
@@ -309,22 +309,24 @@ const addHeader = () => { | |
try { | ||
logger.info(t('Adding header to {0}', editor.document.fileName)) | ||
const ext = path.extname(editor.document.fileName) | ||
const style = getApplyStyle(config.get('style', {}), ext) | ||
|
||
const cfg = config() | ||
const style = getApplyStyle(cfg.get('style', {}), ext) | ||
if (!style) { | ||
return | ||
} | ||
|
||
const headerRange = getHeaderRange(editor.document, style) | ||
|
||
const commentElements = config.get('commentElements', []) | ||
const commentElementsValue = config.get('commentElementsValue', {}) | ||
const customCommentElementsValue = config.get('customCommentElementsValue', {}) | ||
const commentElements = cfg.get('commentElements', []) | ||
const commentElementsValue = cfg.get('commentElementsValue', {}) | ||
const customCommentElementsValue = cfg.get('customCommentElementsValue', {}) | ||
const oldCommentElementsValue = {} as ahp.CommentElementsValues | ||
for (const element of commentElements) { | ||
const elementValue = getElementValue(editor.document, headerRange, style, element) | ||
oldCommentElementsValue[element] = elementValue | ||
} | ||
const dateFormate = config.get('dateFormate', 'YYYY-MM-DD HH:mm:ss') | ||
const dateFormate = cfg.get('dateFormate', 'YYYY-MM-DD HH:mm:ss') | ||
|
||
let headerText = genNewHeader( | ||
editor.document, | ||
|
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 |
---|---|---|
@@ -1,12 +1,28 @@ | ||
/****************************************************************************** | ||
* @Author : Robert Huang<[email protected]> * | ||
* @CreatedDate : 2023-02-07 15:29:47 * | ||
* @LastEditors : Robert Huang<[email protected]> * | ||
* @LastEditDate : 2023-02-07 15:29:47 * | ||
* @FilePath : auto-header-plus/src/test/suite/utils.test.ts * | ||
* @CopyRight : MerBleueAviation * | ||
*****************************************************************************/ | ||
|
||
import * as assert from 'assert' | ||
import dayjs from 'dayjs' | ||
import * as vscode from 'vscode' | ||
// You can import and use all API from the 'vscode' module | ||
// as well as import your extension to test it | ||
import { config } from '../../extension' | ||
import { executeCommand, getApplyStyle, getDateValue, getFinalStringContainsCmd, getPathValue } from '../../utils' | ||
import { | ||
config, | ||
executeCommand, | ||
getApplyStyle, | ||
getDateValue, | ||
getFinalStringContainsCmd, | ||
getPathValue | ||
} from '../../utils' | ||
import { styleC, stylePy } from '../config' | ||
|
||
const cfg = config() | ||
suite('Utils Test Suite', () => { | ||
test('executeCommand test', () => { | ||
assert.strictEqual(executeCommand('echo TEST'), 'TEST') | ||
|
@@ -15,10 +31,10 @@ suite('Utils Test Suite', () => { | |
|
||
test('getApplyStyle test', () => { | ||
assert.strictEqual(getApplyStyle({}, 'js'), undefined) | ||
assert.deepStrictEqual(getApplyStyle(config.get('style', {}), '.js'), styleC) | ||
assert.deepStrictEqual(getApplyStyle(config.get('style', {}), 'js'), styleC) | ||
assert.deepStrictEqual(getApplyStyle(config.get('style', {}), 'j'), undefined) | ||
assert.deepStrictEqual(getApplyStyle(config.get('style', {}), 'py'), stylePy) | ||
assert.deepStrictEqual(getApplyStyle(cfg.get('style', {}), '.js'), styleC) | ||
assert.deepStrictEqual(getApplyStyle(cfg.get('style', {}), 'js'), styleC) | ||
assert.deepStrictEqual(getApplyStyle(cfg.get('style', {}), 'j'), undefined) | ||
assert.deepStrictEqual(getApplyStyle(cfg.get('style', {}), 'py'), stylePy) | ||
}) | ||
|
||
test('getDateValue test', () => { | ||
|
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 |
---|---|---|
|
@@ -2,19 +2,23 @@ | |
* @Author : Robert Huang<[email protected]> * | ||
* @CreatedDate : 2023-02-04 20:40:57 * | ||
* @LastEditors : Robert Huang<[email protected]> * | ||
* @LastEditDate : 2023-02-04 20:41:05 * | ||
* @LastEditDate : 2023-02-07 15:22:50 * | ||
* @FilePath : auto-header-plus/src/utils.ts * | ||
* @CopyRight : MerBleueAviation * | ||
*****************************************************************************/ | ||
|
||
import dayjs from 'dayjs' | ||
import * as path from 'path' | ||
import * as vscode from 'vscode' | ||
import { config, logger } from './extension' | ||
import packageJson from '../package.json' | ||
import { logger } from './extension' | ||
|
||
const t = vscode.l10n.t | ||
const execSync = require('child_process').execSync | ||
|
||
const config = () => { | ||
return vscode.workspace.getConfiguration(packageJson.name) | ||
} | ||
/** | ||
* Run command and return result, if error, return empty string | ||
* @param command | ||
|
@@ -95,7 +99,7 @@ const getApplyStyle = (styles: ahp.Styles, ext: string): ahp.StyleRaw | undefine | |
*/ | ||
const isStyleValid = (key: string, style: ahp.StyleRaw): boolean => { | ||
// if turn off style check, always return true | ||
if (config.get('enableStyleCheck') === false) { | ||
if (config().get('enableStyleCheck') === false) { | ||
return true | ||
} | ||
|
||
|
@@ -253,4 +257,4 @@ const splitString = (str: string, width: number): string[] => { | |
} | ||
return arr | ||
} | ||
export { getApplyStyle, executeCommand, getFinalStringContainsCmd, getDateValue, getPathValue, splitString } | ||
export { getApplyStyle, executeCommand, getFinalStringContainsCmd, getDateValue, getPathValue, splitString, config } |