mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-23 00:35:51 +00:00
style: 完成所有文件的lint
This commit is contained in:
@@ -1,36 +1,36 @@
|
||||
import type { ToolResultBlockParam } from '@anthropic-ai/sdk/resources/index.mjs'
|
||||
import { relative } from 'path'
|
||||
import * as React from 'react'
|
||||
import { MessageResponse } from 'src/components/MessageResponse.js'
|
||||
import { extractTag } from 'src/utils/messages.js'
|
||||
import { CtrlOToExpand } from 'src/components/CtrlOToExpand.js'
|
||||
import { FallbackToolUseErrorMessage } from 'src/components/FallbackToolUseErrorMessage.js'
|
||||
import { FileEditToolUpdatedMessage } from 'src/components/FileEditToolUpdatedMessage.js'
|
||||
import { FileEditToolUseRejectedMessage } from 'src/components/FileEditToolUseRejectedMessage.js'
|
||||
import type { ToolResultBlockParam } from '@anthropic-ai/sdk/resources/index.mjs';
|
||||
import { relative } from 'path';
|
||||
import * as React from 'react';
|
||||
import { MessageResponse } from 'src/components/MessageResponse.js';
|
||||
import { extractTag } from 'src/utils/messages.js';
|
||||
import { CtrlOToExpand } from 'src/components/CtrlOToExpand.js';
|
||||
import { FallbackToolUseErrorMessage } from 'src/components/FallbackToolUseErrorMessage.js';
|
||||
import { FileEditToolUpdatedMessage } from 'src/components/FileEditToolUpdatedMessage.js';
|
||||
import { FileEditToolUseRejectedMessage } from 'src/components/FileEditToolUseRejectedMessage.js';
|
||||
|
||||
import { HighlightedCode } from 'src/components/HighlightedCode.js'
|
||||
import { useTerminalSize } from 'src/hooks/useTerminalSize.js'
|
||||
import { Box, Text } from '@anthropic/ink'
|
||||
import { FilePathLink } from 'src/components/FilePathLink.js'
|
||||
import type { ToolProgressData } from 'src/Tool.js'
|
||||
import type { ProgressMessage } from 'src/types/message.js'
|
||||
import { getCwd } from 'src/utils/cwd.js'
|
||||
import { getDisplayPath } from 'src/utils/file.js'
|
||||
import { getPlansDirectory } from 'src/utils/plans.js'
|
||||
import type { Output } from './FileWriteTool.js'
|
||||
import { HighlightedCode } from 'src/components/HighlightedCode.js';
|
||||
import { useTerminalSize } from 'src/hooks/useTerminalSize.js';
|
||||
import { Box, Text } from '@anthropic/ink';
|
||||
import { FilePathLink } from 'src/components/FilePathLink.js';
|
||||
import type { ToolProgressData } from 'src/Tool.js';
|
||||
import type { ProgressMessage } from 'src/types/message.js';
|
||||
import { getCwd } from 'src/utils/cwd.js';
|
||||
import { getDisplayPath } from 'src/utils/file.js';
|
||||
import { getPlansDirectory } from 'src/utils/plans.js';
|
||||
import type { Output } from './FileWriteTool.js';
|
||||
|
||||
const MAX_LINES_TO_RENDER = 10
|
||||
const MAX_LINES_TO_RENDER = 10;
|
||||
// Model output uses \n regardless of platform, so always split on \n.
|
||||
// os.EOL is \r\n on Windows, which would give numLines=1 for all files.
|
||||
const EOL = '\n'
|
||||
const EOL = '\n';
|
||||
|
||||
/**
|
||||
* Count visible lines in file content. A trailing newline is treated as a
|
||||
* line terminator (not a new empty line), matching editor line numbering.
|
||||
*/
|
||||
export function countLines(content: string): number {
|
||||
const parts = content.split(EOL)
|
||||
return content.endsWith(EOL) ? parts.length - 1 : parts.length
|
||||
const parts = content.split(EOL);
|
||||
return content.endsWith(EOL) ? parts.length - 1 : parts.length;
|
||||
}
|
||||
|
||||
function FileWriteToolCreatedMessage({
|
||||
@@ -38,14 +38,14 @@ function FileWriteToolCreatedMessage({
|
||||
content,
|
||||
verbose,
|
||||
}: {
|
||||
filePath: string
|
||||
content: string
|
||||
verbose: boolean
|
||||
filePath: string;
|
||||
content: string;
|
||||
verbose: boolean;
|
||||
}): React.ReactNode {
|
||||
const { columns } = useTerminalSize()
|
||||
const contentWithFallback = content || '(No content)'
|
||||
const numLines = countLines(content)
|
||||
const plusLines = numLines - MAX_LINES_TO_RENDER
|
||||
const { columns } = useTerminalSize();
|
||||
const contentWithFallback = content || '(No content)';
|
||||
const numLines = countLines(content);
|
||||
const plusLines = numLines - MAX_LINES_TO_RENDER;
|
||||
|
||||
return (
|
||||
<MessageResponse>
|
||||
@@ -57,12 +57,7 @@ function FileWriteToolCreatedMessage({
|
||||
<Box flexDirection="column">
|
||||
<HighlightedCode
|
||||
code={
|
||||
verbose
|
||||
? contentWithFallback
|
||||
: contentWithFallback
|
||||
.split('\n')
|
||||
.slice(0, MAX_LINES_TO_RENDER)
|
||||
.join('\n')
|
||||
verbose ? contentWithFallback : contentWithFallback.split('\n').slice(0, MAX_LINES_TO_RENDER).join('\n')
|
||||
}
|
||||
filePath={filePath}
|
||||
width={columns - 12}
|
||||
@@ -70,22 +65,19 @@ function FileWriteToolCreatedMessage({
|
||||
</Box>
|
||||
{!verbose && plusLines > 0 && (
|
||||
<Text dimColor>
|
||||
… +{plusLines} {plusLines === 1 ? 'line' : 'lines'}{' '}
|
||||
{numLines > 0 && <CtrlOToExpand />}
|
||||
… +{plusLines} {plusLines === 1 ? 'line' : 'lines'} {numLines > 0 && <CtrlOToExpand />}
|
||||
</Text>
|
||||
)}
|
||||
</Box>
|
||||
</MessageResponse>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export function userFacingName(
|
||||
input: Partial<{ file_path: string; content: string }> | undefined,
|
||||
): string {
|
||||
export function userFacingName(input: Partial<{ file_path: string; content: string }> | undefined): string {
|
||||
if (input?.file_path?.startsWith(getPlansDirectory())) {
|
||||
return 'Updated plan'
|
||||
return 'Updated plan';
|
||||
}
|
||||
return 'Write'
|
||||
return 'Write';
|
||||
}
|
||||
|
||||
/** Gates fullscreen click-to-expand. Only `create` truncates (to
|
||||
@@ -93,24 +85,22 @@ export function userFacingName(
|
||||
* Called per visible message on hover/scroll, so early-exit after finding the
|
||||
* (MAX+1)th line instead of splitting the whole (possibly huge) content. */
|
||||
export function isResultTruncated({ type, content }: Output): boolean {
|
||||
if (type !== 'create') return false
|
||||
let pos = 0
|
||||
if (type !== 'create') return false;
|
||||
let pos = 0;
|
||||
for (let i = 0; i < MAX_LINES_TO_RENDER; i++) {
|
||||
pos = content.indexOf(EOL, pos)
|
||||
if (pos === -1) return false
|
||||
pos++
|
||||
pos = content.indexOf(EOL, pos);
|
||||
if (pos === -1) return false;
|
||||
pos++;
|
||||
}
|
||||
// countLines treats a trailing EOL as a terminator, not a new line
|
||||
return pos < content.length
|
||||
return pos < content.length;
|
||||
}
|
||||
|
||||
export function getToolUseSummary(
|
||||
input: Partial<{ file_path: string; content: string }> | undefined,
|
||||
): string | null {
|
||||
export function getToolUseSummary(input: Partial<{ file_path: string; content: string }> | undefined): string | null {
|
||||
if (!input?.file_path) {
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
return getDisplayPath(input.file_path)
|
||||
return getDisplayPath(input.file_path);
|
||||
}
|
||||
|
||||
export function renderToolUseMessage(
|
||||
@@ -118,49 +108,38 @@ export function renderToolUseMessage(
|
||||
{ verbose }: { verbose: boolean },
|
||||
): React.ReactNode {
|
||||
if (!input.file_path) {
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
// For plan files, path is already in userFacingName
|
||||
if (input.file_path.startsWith(getPlansDirectory())) {
|
||||
return ''
|
||||
return '';
|
||||
}
|
||||
return (
|
||||
<FilePathLink filePath={input.file_path}>
|
||||
{verbose ? input.file_path : getDisplayPath(input.file_path)}
|
||||
</FilePathLink>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export function renderToolUseRejectedMessage(
|
||||
{ file_path }: { file_path: string; content: string },
|
||||
{ style, verbose }: { style?: 'condensed'; verbose: boolean },
|
||||
): React.ReactNode {
|
||||
return (
|
||||
<FileEditToolUseRejectedMessage
|
||||
file_path={file_path}
|
||||
operation="write"
|
||||
style={style}
|
||||
verbose={verbose}
|
||||
/>
|
||||
)
|
||||
return <FileEditToolUseRejectedMessage file_path={file_path} operation="write" style={style} verbose={verbose} />;
|
||||
}
|
||||
|
||||
export function renderToolUseErrorMessage(
|
||||
result: ToolResultBlockParam['content'],
|
||||
{ verbose }: { verbose: boolean },
|
||||
): React.ReactNode {
|
||||
if (
|
||||
!verbose &&
|
||||
typeof result === 'string' &&
|
||||
extractTag(result, 'tool_use_error')
|
||||
) {
|
||||
if (!verbose && typeof result === 'string' && extractTag(result, 'tool_use_error')) {
|
||||
return (
|
||||
<MessageResponse>
|
||||
<Text color="error">Error writing file</Text>
|
||||
</MessageResponse>
|
||||
)
|
||||
);
|
||||
}
|
||||
return <FallbackToolUseErrorMessage result={result} verbose={verbose} />
|
||||
return <FallbackToolUseErrorMessage result={result} verbose={verbose} />;
|
||||
}
|
||||
|
||||
export function renderToolResultMessage(
|
||||
@@ -170,7 +149,7 @@ export function renderToolResultMessage(
|
||||
): React.ReactNode {
|
||||
switch (type) {
|
||||
case 'create': {
|
||||
const isPlanFile = filePath.startsWith(getPlansDirectory())
|
||||
const isPlanFile = filePath.startsWith(getPlansDirectory());
|
||||
|
||||
// Plan files: invert condensed behavior
|
||||
// - Regular mode: just show hint (user can type /plan to see full content)
|
||||
@@ -181,28 +160,21 @@ export function renderToolResultMessage(
|
||||
<MessageResponse>
|
||||
<Text dimColor>/plan to preview</Text>
|
||||
</MessageResponse>
|
||||
)
|
||||
);
|
||||
}
|
||||
} else if (style === 'condensed' && !verbose) {
|
||||
const numLines = countLines(content)
|
||||
const numLines = countLines(content);
|
||||
return (
|
||||
<Text>
|
||||
Wrote <Text bold>{numLines}</Text> lines to{' '}
|
||||
<Text bold>{relative(getCwd(), filePath)}</Text>
|
||||
Wrote <Text bold>{numLines}</Text> lines to <Text bold>{relative(getCwd(), filePath)}</Text>
|
||||
</Text>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<FileWriteToolCreatedMessage
|
||||
filePath={filePath}
|
||||
content={content}
|
||||
verbose={verbose}
|
||||
/>
|
||||
)
|
||||
return <FileWriteToolCreatedMessage filePath={filePath} content={content} verbose={verbose} />;
|
||||
}
|
||||
case 'update': {
|
||||
const isPlanFile = filePath.startsWith(getPlansDirectory())
|
||||
const isPlanFile = filePath.startsWith(getPlansDirectory());
|
||||
return (
|
||||
<FileEditToolUpdatedMessage
|
||||
filePath={filePath}
|
||||
@@ -211,7 +183,7 @@ export function renderToolResultMessage(
|
||||
verbose={verbose}
|
||||
previewHint={isPlanFile ? '/plan to preview' : undefined}
|
||||
/>
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user