Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {List} from './List.js'
import {UserInput} from './UserInput.js'
import {FilePath} from './FilePath.js'
import {Subdued} from './Subdued.js'
import {isCharToken} from './token-item.js'
import {LinksContext} from '../contexts/LinksContext.js'
import React, {FunctionComponent, useContext} from 'react'
import {Box, Text} from 'ink'
Expand Down Expand Up @@ -42,7 +43,7 @@ const InlineBlocks: React.FC<{blocks: Block[]}> = ({blocks}) => {
<Text>
{blocks.map((block, blockIndex) => (
<Text key={blockIndex}>
{blockIndex !== 0 && !(typeof block.value !== 'string' && 'char' in block.value) && <Text> </Text>}
{blockIndex !== 0 && !isCharToken(block.value) && <Text> </Text>}
<TokenizedText item={block.value} />
</Text>
))}
Expand Down
12 changes: 10 additions & 2 deletions packages/cli-kit/src/private/node/ui/components/token-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ export type Token =
export type InlineToken = Exclude<Token, ListToken>
export type TokenItem<T extends Token = Token> = T | T[]

/**
* Char tokens hold punctuation that belongs to the preceding token, so they are
* rendered flush against it instead of being separated by a space.
*/
export function isCharToken(token: Token): boolean {
return typeof token !== 'string' && 'char' in token
}

export function tokenItemToString(token: TokenItem): string {
if (typeof token === 'string') {
return token
Expand Down Expand Up @@ -90,7 +98,7 @@ export function tokenItemToString(token: TokenItem): string {
} else {
return token
.map((item, index) => {
if (index !== 0 && !(typeof item !== 'string' && 'char' in item)) {
if (index !== 0 && !isCharToken(item)) {
return ` ${tokenItemToString(item)}`
} else {
return tokenItemToString(item)
Expand All @@ -111,7 +119,7 @@ export function tokenItemToJsonString(token: TokenItem): string {
return token
.map((item, index) => {
const value = tokenToJsonString(item)
const needsLeadingSpace = index !== 0 && !(typeof item !== 'string' && 'char' in item)
const needsLeadingSpace = index !== 0 && !isCharToken(item)
return needsLeadingSpace ? ` ${value}` : value
})
.join('')
Expand Down