变更项目结构
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
// ============================================================
|
||||
// 第二阶段练习文件 — 迷宫导航
|
||||
// ============================================================
|
||||
//
|
||||
// 练习 1:单词跳跃
|
||||
// 用 w 从 "START" 跳到 "END",然后用 b 跳回来
|
||||
// 例如:按 w w w w ... 按 b b b b ...
|
||||
//
|
||||
// 练习 2:使用 f 查找
|
||||
// 用 fX 跳到标记 X 的位置,然后按 ; 到下一个 X
|
||||
// 例如:fX → ; → ; → ; → ...
|
||||
//
|
||||
// 练习 3:用 / 搜索
|
||||
// 用 /TODO 找到所有 TODO 标记,用 n 跳到下一个
|
||||
//
|
||||
// 练习 4:用 % 匹配括号
|
||||
// 跳到任意 {}()[] 上按 %
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
// START --X-- word1 word2 word3 --X-- word4 word5
|
||||
// --X-- word6 word7 word8 --X-- word9 word10
|
||||
|
||||
function calc(a: number, b: number): number {
|
||||
// TODO: implement complex calculation
|
||||
if (a > b) {
|
||||
return a + b; // --X-- destination
|
||||
}
|
||||
return b - a; // --X-- another point
|
||||
}
|
||||
|
||||
interface Data {
|
||||
id: number;
|
||||
name: string;
|
||||
tags: string[];
|
||||
// TODO: add metadata field
|
||||
}
|
||||
|
||||
const config = {
|
||||
server: "localhost",
|
||||
port: 3000,
|
||||
debug: true,
|
||||
// TODO: add production config
|
||||
};
|
||||
|
||||
// --X-- mid1 --X-- mid2 --X-- mid3
|
||||
|
||||
class Something {
|
||||
items: string[];
|
||||
|
||||
constructor(items: string[]) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
// TODO: implement filter
|
||||
filter(predicate: (item: string) => boolean): string[] {
|
||||
return this.items.filter(predicate);
|
||||
}
|
||||
}
|
||||
|
||||
// --X-- near-end-1 --X-- near-end-2
|
||||
// END
|
||||
@@ -0,0 +1,66 @@
|
||||
const hello = "Hello, Zed Vim!";
|
||||
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
|
||||
function greet(name: string): string {
|
||||
return `Hello, ${name}! Welcome to Vim mode.`;
|
||||
}
|
||||
|
||||
function sumArray(arr: number[]): number {
|
||||
let total = 0;
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
total += arr[i];
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
class User {
|
||||
name: string;
|
||||
email: string;
|
||||
isActive: boolean;
|
||||
|
||||
constructor(name: string, email: string) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.isActive = true;
|
||||
}
|
||||
|
||||
deactivate(): void {
|
||||
this.isActive = false;
|
||||
}
|
||||
|
||||
getDisplayName(): string {
|
||||
return `${this.name} <${this.email}>`;
|
||||
}
|
||||
}
|
||||
|
||||
const user = new User("Alice", "alice@example.com");
|
||||
console.log(greet(user.name));
|
||||
console.log("Sum:", sumArray(numbers));
|
||||
console.log("Active:", user.isActive);
|
||||
|
||||
const users = [
|
||||
{ id: 1, name: "Alice", age: 30 },
|
||||
{ id: 2, name: "Bob", age: 17 },
|
||||
{ id: 3, name: "Carol", age: 25 },
|
||||
];
|
||||
|
||||
function getUserNames(list: any[]) {
|
||||
const names = [];
|
||||
for (const item of list) {
|
||||
names.push(item.nam); // ← 问题 2:拼写错误,nam 应该是 name
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
function getAdultUsers(list: any[]) {
|
||||
return list.filter((u) => u.age < 18); // ← 问题 3:条件写反了,应该是 >= 18
|
||||
}
|
||||
|
||||
console.log("debug: users =", users); // ← 问题 1:调试残留,上线前删掉
|
||||
|
||||
const names = getUserNames(users);
|
||||
const adults = getAdultUsers(users);
|
||||
console.log(names);
|
||||
console.log(adults);
|
||||
|
||||
export {};
|
||||
@@ -0,0 +1,49 @@
|
||||
// ============================================================
|
||||
// 模拟项目 — src/calculator.ts
|
||||
// ============================================================
|
||||
|
||||
export class Calculator {
|
||||
add(a: number, b: number): number {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
subtract(a: number, b: number): number {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
multiply(a: number, b: number): number {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
divide(a: number, b: number): number {
|
||||
if (b === 0) {
|
||||
throw new Error("Division by zero");
|
||||
}
|
||||
return a / b;
|
||||
}
|
||||
|
||||
power(base: number, exponent: number): number {
|
||||
let result = 1;
|
||||
for (let i = 0; i < exponent; i++) {
|
||||
result *= base;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
factorial(n: number): number {
|
||||
if (n < 0) {
|
||||
throw new Error("Factorial of negative number");
|
||||
}
|
||||
if (n === 0 || n === 1) {
|
||||
return 1;
|
||||
}
|
||||
let result = 1;
|
||||
for (let i = 2; i <= n; i++) {
|
||||
result *= i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
let c = new Calculator();
|
||||
c.power(1, 2);
|
||||
@@ -0,0 +1,40 @@
|
||||
// ============================================================
|
||||
// 模拟项目 — src/formatter.ts
|
||||
// ============================================================
|
||||
|
||||
export interface FormatOptions {
|
||||
decimals: number;
|
||||
prefix: string;
|
||||
suffix: string;
|
||||
locale: string;
|
||||
}
|
||||
|
||||
export class Formatter {
|
||||
format(value: number, options: FormatOptions): string {
|
||||
const { decimals, prefix, suffix, locale } = options;
|
||||
const formatted = value.toLocaleString(locale, {
|
||||
minimumFractionDigits: decimals,
|
||||
maximumFractionDigits: decimals,
|
||||
});
|
||||
return `${prefix}${formatted}${suffix}`;
|
||||
}
|
||||
|
||||
formatPercent(value: number, decimals: number = 1): string {
|
||||
return `${(value * 100).toFixed(decimals)}%`;
|
||||
}
|
||||
|
||||
formatCurrency(value: number, currency: string = "USD"): string {
|
||||
return value.toLocaleString("en-US", {
|
||||
style: "currency",
|
||||
currency,
|
||||
});
|
||||
}
|
||||
|
||||
truncate(value: number, maxLength: number): string {
|
||||
const str = value.toString();
|
||||
if (str.length <= maxLength) {
|
||||
return str;
|
||||
}
|
||||
return str.slice(0, maxLength) + "...";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// ============================================================
|
||||
// 模拟项目 — src/index.ts
|
||||
// 练习项目导航和重构的入口文件
|
||||
// ============================================================
|
||||
|
||||
import { Calculator } from "./calculator";
|
||||
import { Formatter, type FormatOptions } from "./formatter";
|
||||
import { DataStore } from "./store";
|
||||
import { logger, LogLevel } from "./logger";
|
||||
|
||||
// TODO: Add more test cases
|
||||
// FIXME: Handle edge cases for negative numbers
|
||||
|
||||
async function main(): Promise<void> {
|
||||
logger.log(LogLevel.INFO, "Starting application...");
|
||||
|
||||
try {
|
||||
const calc = new Calculator();
|
||||
const store = new DataStore();
|
||||
const formatter = new Formatter();
|
||||
|
||||
const a = 42;
|
||||
const b = 8;
|
||||
|
||||
const sumResult = calc.add(a, b);
|
||||
const productResult = calc.multiply(a, b);
|
||||
const quotientResult = calc.divide(a, b);
|
||||
const differenceResult = calc.subtract(a, b);
|
||||
|
||||
const options: FormatOptions = {
|
||||
decimals: 2,
|
||||
prefix: "$",
|
||||
suffix: "",
|
||||
locale: "en-US",
|
||||
};
|
||||
|
||||
logger.log(LogLevel.INFO, `Sum: ${sumResult}`);
|
||||
logger.log(LogLevel.INFO, `Product: ${productResult}`);
|
||||
logger.log(LogLevel.INFO, `Quotient: ${formatter.format(quotientResult, options)}`);
|
||||
logger.log(LogLevel.INFO, `Difference: ${differenceResult}`);
|
||||
|
||||
await store.save("last_calculation", {
|
||||
sum: sumResult,
|
||||
product: productResult,
|
||||
quotient: quotientResult,
|
||||
difference: differenceResult,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
|
||||
logger.log(LogLevel.INFO, "Application finished successfully.");
|
||||
} catch (error) {
|
||||
logger.log(LogLevel.ERROR, `Application error: ${error}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
@@ -0,0 +1,73 @@
|
||||
// ============================================================
|
||||
// 模拟项目 — src/utils/logger.ts
|
||||
// ============================================================
|
||||
|
||||
export enum LogLevel {
|
||||
DEBUG = 0,
|
||||
INFO = 1,
|
||||
WARN = 2,
|
||||
ERROR = 3,
|
||||
}
|
||||
|
||||
interface LogEntry {
|
||||
level: LogLevel;
|
||||
message: string;
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
export class Logger {
|
||||
private entries: LogEntry[] = [];
|
||||
private minLevel: LogLevel;
|
||||
|
||||
constructor(minLevel: LogLevel = LogLevel.INFO) {
|
||||
this.minLevel = minLevel;
|
||||
}
|
||||
|
||||
log(level: LogLevel, message: string): void {
|
||||
if (level < this.minLevel) {
|
||||
return;
|
||||
}
|
||||
|
||||
const entry: LogEntry = {
|
||||
level,
|
||||
message,
|
||||
timestamp: new Date(),
|
||||
};
|
||||
|
||||
this.entries.push(entry);
|
||||
|
||||
const levelName = LogLevel[level];
|
||||
const time = entry.timestamp.toISOString();
|
||||
console.log(`[${time}] [${levelName}] ${message}`);
|
||||
}
|
||||
|
||||
debug(message: string): void {
|
||||
this.log(LogLevel.DEBUG, message);
|
||||
}
|
||||
|
||||
info(message: string): void {
|
||||
this.log(LogLevel.INFO, message);
|
||||
}
|
||||
|
||||
warn(message: string): void {
|
||||
this.log(LogLevel.WARN, message);
|
||||
}
|
||||
|
||||
error(message: string): void {
|
||||
this.log(LogLevel.ERROR, message);
|
||||
}
|
||||
|
||||
getEntries(): LogEntry[] {
|
||||
return [...this.entries];
|
||||
}
|
||||
|
||||
getEntriesByLevel(level: LogLevel): LogEntry[] {
|
||||
return this.entries.filter((entry) => entry.level === level);
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.entries = [];
|
||||
}
|
||||
}
|
||||
|
||||
export const logger = new Logger(LogLevel.DEBUG);
|
||||
@@ -0,0 +1,53 @@
|
||||
// ============================================================
|
||||
// 模拟项目 — src/store.ts
|
||||
// ============================================================
|
||||
|
||||
interface StoreEntry {
|
||||
key: string;
|
||||
value: unknown;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export class DataStore {
|
||||
private data: Map<string, StoreEntry> = new Map();
|
||||
|
||||
async save(key: string, value: unknown): Promise<void> {
|
||||
const entry: StoreEntry = {
|
||||
key,
|
||||
value,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
// Simulate async storage operation
|
||||
await new Promise<void>((resolve) => {
|
||||
setTimeout(() => {
|
||||
this.data.set(key, entry);
|
||||
resolve();
|
||||
}, 10);
|
||||
});
|
||||
}
|
||||
|
||||
async load(key: string): Promise<unknown | undefined> {
|
||||
const entry = this.data.get(key);
|
||||
if (!entry) {
|
||||
return undefined;
|
||||
}
|
||||
return entry.value;
|
||||
}
|
||||
|
||||
async delete(key: string): Promise<boolean> {
|
||||
return this.data.delete(key);
|
||||
}
|
||||
|
||||
async list(): Promise<StoreEntry[]> {
|
||||
return Array.from(this.data.values());
|
||||
}
|
||||
|
||||
async clear(): Promise<void> {
|
||||
this.data.clear();
|
||||
}
|
||||
|
||||
getCount(): number {
|
||||
return this.data.size;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// ============================================================
|
||||
// 第三阶段练习文件 — 操作符沙盒
|
||||
// ============================================================
|
||||
//
|
||||
// 在此文件中疯狂练习操作符+文本对象的组合!
|
||||
//
|
||||
// 推荐练习顺序:
|
||||
// 1. ciw / ci" / ci( / ci{ — 修改各种内容
|
||||
// 2. diw / di" / di( / di{ — 删除各种内容
|
||||
// 3. yiw / yi" / yi( / yi{ — 复制各种内容
|
||||
// 4. daw / da" / da( / da{ — 带边界的操作
|
||||
//
|
||||
// ⚠️ 做完后不要保存!用 :q! 退出保持原样
|
||||
// ============================================================
|
||||
|
||||
const greeting = "Hello, World!"; // 用 ci" 修改引号内容
|
||||
|
||||
const items = [1, 2, 3, 4, 5]; // 用 ci[ 修改数组内容
|
||||
|
||||
function multiply(a: number, b: number): number {
|
||||
// 用 ci( 修改参数 → 用 ci{ 修改函数体
|
||||
return a * b;
|
||||
}
|
||||
|
||||
const config = {
|
||||
host: "localhost",
|
||||
port: 8080,
|
||||
debug: true,
|
||||
}; // 用 ci{ 修改整个对象
|
||||
|
||||
// --- 段落练习 ---
|
||||
|
||||
// 用 dap 删除下面这个段落(包括空行)
|
||||
|
||||
This is a paragraph that you can delete with dap.
|
||||
It contains multiple lines of text.
|
||||
Try dap on this!
|
||||
|
||||
// 用 yap 复制上面这个段落
|
||||
|
||||
// --- 复杂嵌套练习 ---
|
||||
|
||||
function processData(input: { name: string; values: number[] }) {
|
||||
const result = input.values.map((value) => value * 2); // 试试 ci( 改箭头函数
|
||||
return {
|
||||
name: input.name.toUpperCase(),
|
||||
doubled: result,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
} // 试试 da{ 把整个 return 对象删掉
|
||||
|
||||
// --- 引号练习 ---
|
||||
const singleQuoted = 'single quotes here'; // ci'
|
||||
const doubleQuoted = "double quotes here"; // ci"
|
||||
const templateStr = `template literal here`; // ci`
|
||||
const mixed = "Hello, 'world'!"; // 试试 i" 和 i'
|
||||
|
||||
// --- 标签练习 (HTML in JSX) ---
|
||||
const element = (
|
||||
<div className="container">
|
||||
<h1>Title Here</h1> {/* cit 修改标签内容 */}
|
||||
<p>Paragraph text</p> {/* dit 删除标签内容 */}
|
||||
</div>
|
||||
); {/* dat 删除整个 div+内容 */}
|
||||
|
||||
export {};
|
||||
@@ -0,0 +1,105 @@
|
||||
// ============================================================
|
||||
// 第四阶段练习文件 — Zed 超能力
|
||||
// ============================================================
|
||||
//
|
||||
// 练习 LSP 导航:
|
||||
// - 光标放在 fetchUsers 上,按 g d 跳转到定义
|
||||
// - 光标放在 User 上,按 g A 查看所有引用
|
||||
// - 光标放在 processUsers 上,按 c d 重命名
|
||||
//
|
||||
// 练习 Tree-sitter 导航:
|
||||
// - 用 ] m / [ m 在方法间跳转
|
||||
// - 用 ] ] / [ [ 在类/接口间跳转
|
||||
// - 用 g c 选择注释
|
||||
//
|
||||
// 练习多光标:
|
||||
// - 光标放在 user 上,按 g l 选中所有 user
|
||||
// - 然后修改它们看看多光标效果
|
||||
//
|
||||
// ============================================================
|
||||
|
||||
interface Address {
|
||||
street: string;
|
||||
city: string;
|
||||
zipCode: string;
|
||||
}
|
||||
|
||||
interface User {
|
||||
id: number;
|
||||
name: string;
|
||||
email: string;
|
||||
isActive: boolean;
|
||||
address?: Address;
|
||||
tags: string[];
|
||||
}
|
||||
|
||||
class UserService {
|
||||
private users: User[] = [];
|
||||
|
||||
async fetchUsers(): Promise<User[]> {
|
||||
// TODO: Connect to API
|
||||
const response = await fetch("/api/users");
|
||||
const data = await response.json();
|
||||
this.users = data;
|
||||
return this.users;
|
||||
}
|
||||
|
||||
findUserById(id: number): User | undefined {
|
||||
return this.users.find((user) => user.id === id);
|
||||
}
|
||||
|
||||
getActiveUsers(): User[] {
|
||||
return this.users.filter((user) => user.isActive);
|
||||
}
|
||||
|
||||
addTagToUser(userId: number, tag: string): void {
|
||||
const user = this.findUserById(userId);
|
||||
if (user) {
|
||||
user.tags.push(tag);
|
||||
}
|
||||
}
|
||||
|
||||
removeTagFromUser(userId: number, tag: string): void {
|
||||
const user = this.findUserById(userId);
|
||||
if (user) {
|
||||
user.tags = user.tags.filter((t) => t !== tag);
|
||||
}
|
||||
}
|
||||
|
||||
// 试试用 da f 删除整个方法,用 ci f 修改方法体
|
||||
getUsersByCity(city: string): User[] {
|
||||
return this.users.filter((user) => user.address?.city === city);
|
||||
}
|
||||
}
|
||||
|
||||
class NotificationService {
|
||||
sendEmail(user: User, subject: string, body: string): void {
|
||||
// Send email logic
|
||||
console.log(`Sending email to ${user.email}: ${subject}`);
|
||||
}
|
||||
|
||||
notifyActiveUsers(users: User[], message: string): void {
|
||||
const activeUsers = users.filter((user) => user.isActive);
|
||||
activeUsers.forEach((user) => {
|
||||
this.sendEmail(user, "Notification", message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Main application
|
||||
async function main(): Promise<void> {
|
||||
const userService = new UserService();
|
||||
const notificationService = new NotificationService();
|
||||
|
||||
const users = await userService.fetchUsers();
|
||||
const activeUsers = userService.getActiveUsers();
|
||||
|
||||
notificationService.notifyActiveUsers(users, "Welcome back!");
|
||||
|
||||
console.log(`Total users: ${users.length}`);
|
||||
console.log(`Active users: ${activeUsers.length}`);
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
|
||||
export {};
|
||||
@@ -0,0 +1,65 @@
|
||||
// ============================================================
|
||||
// 第五阶段练习文件 — 高级技巧工坊
|
||||
// ============================================================
|
||||
|
||||
// === 练习 1:Surround(环绕) ===
|
||||
// 把下面的单词用不同方式包裹起来
|
||||
const bareWord = target; // ysiw" → "target"
|
||||
const singleQuoted = 'hello'; // cs'" → "hello"; ds" → hello
|
||||
const tagged = <div>text</div>; // cst<span> → <span>text</span>
|
||||
|
||||
// === 练习 2:寄存器 ===
|
||||
// "ayiw — 复制下面单词到 a 寄存器
|
||||
// "ap — 把它粘贴到别处
|
||||
const important = "DON'T LOSE THIS";
|
||||
|
||||
// "+y — 复制到系统剪贴板 (使用系统剪贴板)
|
||||
// "*p — 从系统剪贴板粘贴
|
||||
const shared = "clipboard test";
|
||||
|
||||
// === 练习 3:宏 ===
|
||||
// 把下面的数组转换成对象数组 [{id: 1}, {id: 2}, {id: 3}]
|
||||
// 1. qa 开始录制
|
||||
// 2. 在第一行编辑:I{id: <Esc>A}<Esc>
|
||||
// 3. j 到下一行
|
||||
// 4. q 停止录制
|
||||
// 5. 2@a 播放两次
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
|
||||
// === 练习 4:可视模式 ===
|
||||
// V — 行选择下面几行
|
||||
// > — 缩进
|
||||
// < — 减少缩进
|
||||
// gv — 重新选择
|
||||
const items = [
|
||||
"apple",
|
||||
"banana",
|
||||
"cherry",
|
||||
"date",
|
||||
"elderberry",
|
||||
];
|
||||
|
||||
// === 练习 5:替换 ===
|
||||
// :%s/alpha/beta/g — 全局替换
|
||||
const message = "alpha alpha alpha alpha";
|
||||
const more = "alpha here, alpha there";
|
||||
|
||||
// === 练习 6:排序 ===
|
||||
// V 选中下面几行 → :sort
|
||||
const unsorted = [
|
||||
"zebra",
|
||||
"apple",
|
||||
"monkey",
|
||||
"banana",
|
||||
"cat",
|
||||
];
|
||||
|
||||
// === 练习 7:标记 ===
|
||||
// ma 设置标记 a — `a 跳回来
|
||||
const bookmark = "important line!";
|
||||
|
||||
export {};
|
||||
Reference in New Issue
Block a user