Skip to content

Commit

Permalink
feat!: support insert empty line base on direction
Browse files Browse the repository at this point in the history
BREAKING: deprecated insertEmptyLineBeforeLogMessage and insertEmptyLineAfterLogMessage, use insertEmptyLine instead
  • Loading branch information
libondev committed Jun 11, 2024
1 parent 4a7629f commit b8a892b
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 53 deletions.
20 changes: 10 additions & 10 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
"presentation": {
"reveal": "never"
},
"problemMatcher": [
{
"base": "$ts-webpack-watch",
"background": {
"activeOnStart": true,
"beginsPattern": "Build start",
"endsPattern": "Build success"
}
}
],
// "problemMatcher": [
// {
// "base": "$ts-webpack-watch",
// "background": {
// "activeOnStart": true,
// "beginsPattern": "Build start",
// "endsPattern": "Build success"
// }
// }
// ],
"group": "build"
}
]
Expand Down
11 changes: 4 additions & 7 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ This document is also available in [English](./README-CN.md).

您可以通过设置个性化您希望插入的语句。(这个插件也可以作为一个简单的代码片段工具。)

```json
```json5
// 默认配置
{
// 执行操作后自动保存当前文件
Expand All @@ -114,11 +114,8 @@ This document is also available in [English](./README-CN.md).
// 某些语言的日志输出方法不支持多个参数,比如:Java
"debugger-for-console.onlyOutputVariable": "auto",

// 是否在日志消息前插入空行
"debugger-for-console.insertEmptyLineBeforeLogMessage": false,

// 是否在日志消息后插入空行
"debugger-for-console.insertEmptyLineAfterLogMessage": false,
// 是否在日志消息前/后插入空行
"debugger-for-console.insertEmptyLine": "none",

// 自定义不同语言的输出语句
"debugger-for-console.wrappers": {
Expand All @@ -136,7 +133,7 @@ This document is also available in [English](./README-CN.md).

### examples

```json
```json5
{
"debugger-for-console.wrappers": {
"javascript": "console.warn({VALUE})"
Expand Down
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Merge multiple cursors on the same line:

You can personalize the statements you wish to insert via the Settings. (This plugin can also serve as a straightforward code snippet tool.)

```json
```json5
// Default Settings
{
// Automatically save the current file after executing an operation
Expand All @@ -114,11 +114,8 @@ You can personalize the statements you wish to insert via the Settings. (This pl
// The log output method in some languages does not support multiple functions, such as `Java`
"debugger-for-console.onlyOutputVariable": "auto",

// Whether to insert an empty line before the log message or not.
"debugger-for-console.insertEmptyLineBeforeLogMessage": false,

// Whether to insert an empty line after the log message or not.
"debugger-for-console.insertEmptyLineAfterLogMessage": false,
// Whether to insert an empty line before or after the log message or not.
"debugger-for-console.insertEmptyLine": "none",

// Customize debugging statements for different languages
"debugger-for-console.wrappers": {
Expand All @@ -136,7 +133,7 @@ You can personalize the statements you wish to insert via the Settings. (This pl

### examples

```json
```json5
{
"debugger-for-console.wrappers": {
"javascript": "console.warn({VALUE})"
Expand Down
33 changes: 24 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,32 @@
"enable"
],
"default": "auto",
"enumDescriptions": [
"Automatically detect the programming language of the current document.",
"Remove all redundant prompts and only output variables."
],
"description": "Set to `enable` to cancel all redundant information and output only variables instead..\nTips: When the log output method of some languages does not support multiple parameters, it will be temporarily switched to `enable` internally, such as `Java`.. If it is a custom function, it should be set to \"enable\"."
},
"debugger-for-console.insertEmptyLineBeforeLogMessage": {
"type": "boolean",
"default": false,
"description": "Whether to insert an empty line before the log message or not."
},
"debugger-for-console.insertEmptyLineAfterLogMessage": {
"type": "boolean",
"default": false,
"description": "Whether to insert an empty line after the log message or not."
"debugger-for-console.insertEmptyLine": {
"type": "string",
"default": "none",
"enum": [
"none",
"before",
"after",
"both",
"direction",
"directionReverse"
],
"enumDescriptions": [
"Do not insert an empty line.",
"Insert an empty line before the debug statement.",
"Insert an empty line after the debug statement.",
"Insert an empty line before and after the debug statement.",
"Insert an empty line according to the direction of the debug statement.",
"Insert an empty line according to the reverse direction of the debug statement."
],
"description": "Insert an empty line before or after the debug statement."
},
"debugger-for-console.wrappers": {
"type": "object",
Expand Down
1 change: 1 addition & 0 deletions playground/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"debugger-for-console.insertEmptyLine": "directionReverse",
"debugger-for-console.wrappers": {
"javascript": "console.log({VALUE})"
}
Expand Down
14 changes: 9 additions & 5 deletions src/commands/create.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Position, WorkspaceEdit, window, workspace } from 'vscode'
import type { TextDocument } from 'vscode'

import { resolvedConfig } from '../extension'
import { autoSave } from '../features/saver'
import { getQuote } from '../features/quote'
import { getEmoji } from '../features/emoji'
import { getLines } from '../features/lines'
import { getLevel } from '../features/level'
import { getScope } from '../features/scope'
import { getSymbols } from '../features/symbols'
import { getOneVariable } from '../features/variable'
import { getOnlyVariable } from '../features/variable'
import { getAfterEmptyLine, getBeforeEmptyLine } from '../features/empty-line'

import {
Expand Down Expand Up @@ -71,7 +72,7 @@ function getStatementGenerator(document: TextDocument, symbols: string) {
} else if (statement.includes('{VALUE}')) {
const [start, ...end] = statement.split('{VALUE}')

if (getOneVariable(document.languageId)) {
if (getOnlyVariable(document.languageId)) {
return (_: number, text: string) => `${start}$3${end.join('')}\n`.replace('$3', text)
}

Expand Down Expand Up @@ -109,6 +110,9 @@ async function create(insertLineOffset: number, displayLineOffset: number) {
const statementGetter = getStatementGenerator(document, scopeSymbols)
let position = new Position(0, 0)

const insertPosition = insertLineOffset > 0 ? 'after' : 'before'
const insertEmptyLineConfigValue = resolvedConfig.get('insertEmptyLine') as string

for (const line in mergedSelections) {
const lineNumber = Number(line)

Expand All @@ -120,11 +124,11 @@ async function create(insertLineOffset: number, displayLineOffset: number) {
workspaceEdit.insert(
uri,
position,
`${getBeforeEmptyLine()}${indents
}${statementGetter(
`${getBeforeEmptyLine(insertEmptyLineConfigValue, insertPosition)
}${indents}${statementGetter(
lineNumber + displayLineOffset,
mergedSelections[line].join(', '),
)}${getAfterEmptyLine()}`,
)}${getAfterEmptyLine(insertEmptyLineConfigValue, insertPosition)}`,
)
}

Expand Down
7 changes: 2 additions & 5 deletions src/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { getQuote } from '../features/quote'
import { getEmoji } from '../features/emoji'
import { getLines } from '../features/lines'
import { getLevel } from '../features/level'
import { getOneVariable } from '../features/variable'
import { getAfterEmptyLine, getBeforeEmptyLine } from '../features/empty-line'
import { getOnlyVariable } from '../features/variable'

export function updateUserConfig() {
Object.assign(
Expand All @@ -20,9 +19,7 @@ export function updateUserConfig() {
getEmoji,
getLines,
getLevel,
getOneVariable,
getBeforeEmptyLine,
getAfterEmptyLine,
getOnlyVariable,
]

deps.forEach(dep => dep.update())
Expand Down
30 changes: 21 additions & 9 deletions src/features/empty-line.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { lazyValue } from '../utils/index'
export function getBeforeEmptyLine(configValue: string, direction: string) {
if (
['before', 'both'].includes(configValue)
|| (configValue === 'direction' && direction === 'before')
|| (configValue === 'directionReverse' && direction === 'after')
) {
return '\n'
}

export const getBeforeEmptyLine = lazyValue(
'insertEmptyLineBeforeLogMessage',
insertBefore => insertBefore ? '\n' : '',
)
return ''
}

export const getAfterEmptyLine = lazyValue(
'insertEmptyLineAfterLogMessage',
insertAfter => insertAfter ? '\n' : '',
)
export function getAfterEmptyLine(configValue: string, direction: string) {
if (
['after', 'both'].includes(configValue)
|| (configValue === 'direction' && direction === 'after')
|| (configValue === 'directionReverse' && direction === 'before')
) {
return '\n'
}

return ''
}
2 changes: 1 addition & 1 deletion src/features/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { lazyValue } from '../utils/index'

const ONLY_OUTPUT_ONE_PARAMETER_LANGUAGE_ID = ['java']

export const getOneVariable = lazyValue<string>(
export const getOnlyVariable = lazyValue<string>(
'onlyOutputVariable',
(isOnlyVariable, languageId) => (
isOnlyVariable === 'enable' ||
Expand Down

0 comments on commit b8a892b

Please sign in to comment.