Skip to content

Commit

Permalink
feat: ✨config active without restart vscode
Browse files Browse the repository at this point in the history
  • Loading branch information
hks2002 committed Feb 7, 2023
1 parent f309cdc commit 75e7e9c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 30 deletions.
10 changes: 4 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
*****************************************************************************/
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand All @@ -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'))
Expand Down
22 changes: 12 additions & 10 deletions src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand All @@ -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,
Expand Down
28 changes: 22 additions & 6 deletions src/test/suite/utils.test.ts
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')
Expand All @@ -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', () => {
Expand Down
12 changes: 8 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 }

0 comments on commit 75e7e9c

Please sign in to comment.