蓝色天空网mbsky.com · 知识 · 实践 · 创造VS Code · 本地阅读预览
教程 / DevOps / 工程化 / VS Code

VSCode 新建文件

讲解利用活动栏与主侧边栏快速切换视图、在资源管理器新建 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 组垂直分栏编辑器标签状态。
运行资源管理器新建 index.html、HTML IntelliSense 建议排序与多标签分栏布局离线校验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

当前源码 SHA256:535d13827bcff02da366df0bb6f18ba904a66e33979beb90d8333ee90a7a16f8

下载本站示例源码

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载VSCode 新建文件实验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

ZIP SHA256:19fc70112631e38d3b71dc57256e49af71e7dc8733c30a5e23b932b7ab5edf1d

常见错误与边界

  • 实际端口、文档资源与应用配置都应和测试条件一致。

练习与解答

在编辑区触发 IntelliSense 建议列表后,应使用哪些按键选择并确认插入候选项?如果希望同时并排查看 HTML 与 CSS 文件应怎么做?

展开参考解答

使用方向键 `↑`/`↓` 选中目标建议后按 `Tab`(或 `Enter`)插入;并排查看可将标签页拖拽至编辑区一侧或使用拆分编辑器功能按垂直/水平方向排列多个窗格。

规范与官方依据