let's begin

从初高中的信息编程比赛,到大学以及研究生的土木工程专业学习,从最初的Pascal,编程算法到之后的C++,C# 程序功能开发,一直对着编程有着割舍不开的联系,准备把之前和之后的汇总一下,记录一下自己的学习过程。
function! AlignVerilogPorts() let s:max_type = 0 let s:max_reg = 0 let s:max_bits = 0 let s:max_name = 0 let s:port_list = [] " 第一阶段:定位端口块范围(保持不变) normal! gg call search('module\s\+\w*(\s*$', 'W') normal! j let start_line = line('.') call search(');', 'W') normal! k let end_line = line('.') let port_block_lines = getline(start_line, end_line) " 第二阶段:解析内容(保持不变) for raw_line in port_block_lines let line = substitute(raw_line, '\s*$', '', '') if line =~ '^\s*//' call add(s:port_list, {'type': 'comment', 'content': line}) continue endif let clean_line = substitute(line, '\s*//.*$', '', '') let clean_line = substitute(clean_line, '^\s*,', '', '') let parts = matchlist(clean_line, \ '^\s*\(input\|output\|inout\)\s\+\(reg\|wire\)\?\s*\(\[.*\]\)\?\s*\(\w\+\)') if !empty(parts) let type = parts[1] let regwire = !empty(parts[2]) ? parts[2] : '' let bits = !empty(parts[3]) ? substitute(parts[3], '\t', ' ', 'g') : '' let name = parts[4] let comment = matchstr(line, '//.*$') let s:max_type = max([s:max_type, len(type)+1]) let s:max_reg = max([s:max_reg, len(regwire)+1]) let s:max_bits = max([s:max_bits, len(bits)]) let s:max_name = max([s:max_name, len(name)]) call add(s:port_list, { \ 'type': 'port', \ 'port_type': type, \ 'regwire': regwire, \ 'bits': bits, \ 'name': name, \ 'comment': comment}) endif endfor " 第三阶段:生成对齐内容(修改关键部分) let formatted = [] let cnt = 0 for item in s:port_list if item.type == 'comment' call add(formatted, item.content) else let port = item let prefix = (cnt == 0) ? ' ' : ' ,' " 各字段基础对齐 let type_str = printf('%-'.s:max_type.'s', port.port_type) let reg_str = printf('%-'.s:max_reg.'s', port.regwire) let bit_str = printf('%-'.s:max_bits.'s', port.bits) " 新增:计算前缀总长度并补足30空格 let leading_part = prefix . type_str . reg_str . bit_str . ' ' let current_length = strlen(leading_part) if current_length < 30 let leading_part .= repeat(' ', 30 - current_length) endif " 组合最终行 let line = leading_part . port.name if !empty(port.comment) let line .= ' ' . port.comment endif let line = substitute(line, '\t', ' ', 'g') call add(formatted, substitute(line, '\s\+$', '', '')) let cnt += 1 endif endfor " 第四阶段:替换原始内容(保持不变) execute start_line.','.end_line.'d' call append(start_line-1, formatted) endfunction " 绑定快捷键(按需设置) " nnoremap <silent> <leader>al :call AlignVerilogPorts()<CR> 根据下面的要求,优化以上脚本。 添加多点注释 其他功能保持不变
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值