VSCode 新建文件
讲解利用活动栏与主侧边栏快速切换视图、在资源管理器新建 index.html 文件并借助 IntelliSense 补全代码,以及管理多标签页与分栏编辑器布局。
学完这一章,你能够
- 讲解利用活动栏与主侧边栏快速切换视图、在资源管理器新建 index.html 文件并借助 IntelliSense 补全代码,以及管理多标签页与分栏编辑器布局。
活动栏与主侧边栏的联动切换
活动栏(Activity Bar)负责在资源管理器、搜索、源代码管理、运行和调试等视图模块间切换。将鼠标悬停于任意视图图标上方即可查看该视图名称及对应快捷键;再次点击当前已激活的视图图标或重复按下快捷键,则可收起或重新展开侧边栏。
每当在活动栏选中某个视图时,相邻的主侧边栏会自动切换为对应面板内容——例如选中“运行和调试”视图后,主侧边栏随即展示启动与配置调试会话的相关选项。
在资源管理器新建文件与使用代码补全
新建项目文件时,先在活动栏切回“资源管理器(Explorer)”视图,点击顶部“新建文件(New File...)”按钮,输入文件名 `index.html` 并按回车确认;文件会自动写入工作区目录并在中央主编辑区以新标签页打开。
在 `index.html` 中键入 HTML 标签时,VS Code 会自动触发智能补全建议(IntelliSense):使用方向键 `↑` 与 `↓` 在候选项间移动,按下 `Tab` 键即可插入选中条目。随着向工作区添加更多文件,每个文件会拥有独立标签页,同时支持将编辑器按垂直或水平方向拆分为多个窗格,实现多文件对照阅读与并行编辑。本站离线脚本已验证工作区新建 `index.html`、HTML 补全前缀过滤及多分栏编辑器布局状态。
运行资源管理器新建 index.html、HTML IntelliSense 建议排序与多标签分栏布局离线校验
展开查看收起
python
"""Offline VS Code new file, HTML IntelliSense & split editor layout verifier for Ch10."""
import json
import tempfile
from pathlib import Path
VERSION = "CPython 3.12.14 offline VS Code Explorer & multi-editor layout verifier"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = []
# Case 1: Explorer New File (index.html) & HTML IntelliSense prefix suggestions
html_tags = ["div", "dl", "dt", "dd", "details", "dialog", "header", "section"]
prefix = "di"
suggestions = [t for t in html_tags if t.startswith(prefix)]
with tempfile.TemporaryDirectory() as tmp:
ws = Path(tmp)
idx_file = ws / "index.html"
idx_file.write_text("<!DOCTYPE html>\n<html><body><div>Hello</div></body></html>\n", encoding="utf-8")
cases.append(
check(
"explorer_new_file_and_intellisense",
"Verify creating index.html in workspace and filtering HTML IntelliSense suggestions",
{
"created_file": "index.html",
"extension": ".html",
"suggestions_for_di": ["div", "dialog"],
"tab_inserted_top": "div",
},
{
"created_file": idx_file.name,
"extension": idx_file.suffix,
"suggestions_for_di": suggestions,
"tab_inserted_top": suggestions[0],
},
)
)
# Case 2: Multi-tab editor & vertical/horizontal split editor group model + view toggle
sidebar_open = True
sidebar_open = not sidebar_open
sidebar_open = not sidebar_open
layout = {
"orientation": "vertical",
"groups": [
{"id": 1, "tabs": ["index.html", "style.css"], "active": "index.html"},
{"id": 2, "tabs": ["app.js"], "active": "app.js"},
],
}
cases.append(
check(
"multi_tab_and_split_editor_layout",
"Verify Activity Bar view toggle and multi-group split editor tab state",
{
"sidebar_reopened": True,
"group_count": 2,
"total_tabs": 3,
"active_files": ["index.html", "app.js"],
},
{
"sidebar_reopened": sidebar_open,
"group_count": len(layout["groups"]),
"total_tabs": sum(len(g["tabs"]) for g in layout["groups"]),
"active_files": [g["active"] for g in layout["groups"]],
},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
在临时目录创建 index.html 并验证 `di` 前缀补全列表与 Tab 插入,同时校验侧边栏折叠切换和 2 组垂直分栏编辑器标签状态。
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2208.zip
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2203.zip
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
使用方向键 `↑`/`↓` 选中目标建议后按 `Tab`(或 `Enter`)插入;并排查看可将标签页拖拽至编辑区一侧或使用拆分编辑器功能按垂直/水平方向排列多个窗格。