Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>nfieldscriptNew to Visual Studio Code? Get it now.
nfieldscript

nfieldscript

jimmyliu1986

|
28 installs
| (0) | Free
Nfield ODIN syntax highlighting and tools extension, originally created by PhoenixHuang2020, now maintained by Jimmy Liu
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

Chinese documentation is available below. Please scroll down.

中文说明在下方,请向下滚动查看。

Update 2026-09-22 (v2.5.2): F12 on a question reference (Q123 / Q{id}) now opens an inline Peek showing the whole question block — the *QUESTION line, the question text and every option — so you can proof-read it without losing your place. Ctrl+Click still jumps exactly as before, and F12 on a variable still jumps to its declaration.

2026-09-22 更新 (v2.5.2):在题目引用(Q123 / Q{id})上按 F12,改为弹出内联 Peek,一次性显示整道题的原始内容 —— 题目行、题面文本与全部选项,不必离开当前行即可逐行核对。Ctrl+点击 的跳转、以及 F12 对变量的跳转,均保持原样。

Update 2026-09-21 (v2.5.0): Added the Check Script command — audits the whole ODIN script and shows the report in a new result window (a webview tab beside the source). Reach it from the editor context menu → 检查脚本, the Command Palette, or Alt+K.

2026-09-21 更新 (v2.5.0):新增「检查脚本」命令 —— 对整份 ODIN 脚本做一次全量体检,结果在新开的结果窗口(Webview 标签页,与源码并排)里显示。入口:编辑器右键菜单 →「检查脚本」、命令面板、Alt+K。

Update 2026-09-03 (v2.3.3): Renamed the misspelled <blub> tag to <blue> (plain blue, non-bold, matching the <red> / <green> / <yellow> family); added the missing ENDBLOCK keyword to IntelliSense (82 commands now); Alt+H (Insert Heading) now validates the selection first and reports an error instead of crashing on short input. Also removed dead code and unused dev dependencies, and the extension now activates when an ODIN file is opened rather than at startup.

2026-09-03 更新 (v2.3.3): 将拼写错误的 <blub> 标签更正为 <blue>(纯蓝不加粗,与 <red> / <green> / <yellow> 系列对齐);补全表补上遗漏的 ENDBLOCK 关键字(现共 82 个命令);Alt+H(生成 Heading)现在先校验选区,选区过短时给出错误提示而非崩溃。另清理了死代码与无用开发依赖,插件改为打开 ODIN 文件时激活,而非每次启动都激活。

Update 2026-09-02 (v2.3.0): Added ODIN command keyword completion — 82 built-in commands (QUESTION, IF, GOTO, VARS, etc.) now appear in IntelliSense, triggered by * or while typing.

2026-09-02 更新 (v2.3.0): 新增 ODIN 命令关键字补全 — 82 个内置命令(QUESTION、IF、GOTO、VARS 等)现在出现在智能提示中,输入 * 或键入字母时触发。

Update 2026-08-31 (v2.2.8): Added IntelliSense completion — while typing, variables declared via *VARS, *TEXTVARS, *SAMPLEDATA and question IDs from *ID "xxx" are now suggested.

2026-08-31 更新 (v2.2.8): 新增补全功能 — 键入时自动提示 *VARS、*TEXTVARS、*SAMPLEDATA 声明的变量名,以及 *ID "xxx" 中的题目 ID。

Update 2026-08-27 (v2.2.2): Fixed Find All References (Shift+F12) to distinguish variable references by context — plain text in double-quoted strings is no longer falsely matched; pipe-syntax (*? var) inside strings and variables inside [...] expressions are correctly recognized.

2026-08-27 更新 (v2.2.2): 修复查找所有引用 (Shift+F12) 的上下文判断 — 双引号内的普通文本不再被误判为变量引用;字符串内的管道语法 (*? var) 和方括号 [...] 表达式内的变量现在能被正确识别。

Update 2026-08-28 (v2.2.3): Added STR snippet for all string functions reference (STRLENGTH, STRSUBSTR, STRINDEX, STRREPLACE, SPLITSTRING, JOINSTRING, etc.).

2026-08-28 更新 (v2.2.3): 新增 STR 代码片段,包含全部字符串函数参考(STRLENGTH、STRSUBSTR、STRINDEX、STRREPLACE、SPLITSTRING、JOINSTRING 等)。

Update 2026-08-30 (v2.2.4): Fixed Alt+1 / Alt+2 / Alt+3 tag wrapping to apply to all selected occurrences (multi-cursor, e.g. after Ctrl+F2), not just the primary selection.

2026-08-30 更新 (v2.2.4): 修复 Alt+1 / Alt+2 / Alt+3 标签包裹仅作用于主选区的问题,现在会对所有选中项(多光标,例如 Ctrl+F2 全选同名后)同时添加标签。

Update 2026-08-30 (v2.2.5): Fixed question ID lookup so hover preview and go-to-definition no longer scan past blank lines or question text, preventing Q{...} from wrongly matching the previous question that has no *ID.

2026-08-30 更新 (v2.2.5): 修复题目 ID 查找逻辑,悬停预览与跳转定义不再跨过空行或题目标题行,避免 Q{...} 误匹配到上一题缺少 *ID 的占位题目。

Update 2026-08-30 (v2.2.6): Added visual styling for <blueb>, <redb>, <b> tags — content now displays bold with blueb in blue (#1b68ed), redb in red (#b30904).

2026-08-30 更新 (v2.2.6): 新增 <blueb>、<redb>、<b> 标签可视化样式 — 内容显示为加粗,blueb 蓝色 (#1b68ed),redb 红色 (#b30904)。

Update 2026-08-30 (v2.2.7): Added tag colors for <blue> (blue), <red> (red), <green> (green), <greenb> (green bold), <yellow> (yellow), <yellowb> (yellow bold). The <blue> tag was misspelled <blub> until v2.3.3, when it was renamed and changed to non-bold.

2026-08-30 更新 (v2.2.7): 新增 <blue>(蓝)、<red>(红)、<green>(绿)、<greenb>(绿加粗)、<yellow>(黄)、<yellowb>(黄加粗)标签颜色。<blue> 在 v2.3.3 之前被误拼为 <blub>,该版本已更名为 <blue> 并改为不加粗。


Nfield ODIN Extension

A VS Code extension for Nfield ODIN script syntax highlighting and tools.

About

This extension was originally created by PhoenixHuang2020 and is now maintained by Jimmy Liu with the original author's permission.

Email:jimmy.liu@kantar.com

Authorization

The original author PhoenixHuang2020 has granted Jimmy Liu the authorization to continue maintaining and modifying this extension.

Features

1. Syntax Highlighting

Provided by syntaxes/nfieldodin.tmLanguage.json (TextMate grammar).

Element Pattern Example
Keywords * followed by an ASCII identifier — first character a letter, _ or ?, so CJK and * + digits do not match (*XXX中文, *5 inside (100ml*5)) *QUESTION, *VAR, *IF, *REPEAT
Comments ** at any position ** This is a comment
Strings Double-quoted "varName", "Hello world"
Question references Q + number or Q{IDNAME} Q123, Q{satisfaction}
Option codes Number followed by colon 1:Yes, 02:No
Positions Digit + L + digit, or L + digit, optionally followed by . + digits — word-bounded, only inside a *QUESTION / *FIELD block (follows editor theme color) 1L5, L20, 61L3, L3.2, 64L4.2, *FIELD "61L1"
Built-in functions Predefined function names — word-bounded JSON, PROPERTY, DATETIMEADD, STRLENGTH, TO
Question numbers Number after QUESTION *QUESTION 123 → 123 highlighted

2. Language Configuration

Provided by nfieldodin-configuration.json.

  • Line comment: ** — use Ctrl+/ to toggle
  • Bracket matching: {}, [], ()
  • Auto-closing pairs: {}, [], (), ", ', *REPEAT/*ENDREP, *BLOCK/*ENDBLOCK
  • Surrounding pairs: {}, [], (), ", '

3. Outline View (Document Symbols)

Provided by out/documentSymbolProvider.js.

  • Shows all *QUESTION definitions in the outline panel
  • Question name format: Q123(varName) {idName}
    • (varName) — from *VAR attribute
    • [dimvarName] — from DIMVAR attribute
    • {idName} — from *ID attribute
  • Different icons for dummy questions (*DUMMY) vs normal questions
  • Shows *SUBROUTINE definitions
  • Shows *LIST definitions as List_listName
  • Shows option codes (1:Yes) and *USELIST as child items under each question/list
  • A *LIST block ends at *ENDLIST, a blank line, the next *LIST, or another command that starts with * + a letter (shared rules with the hover provider, see out/listUtils.js). Blank lines between the *LIST line and its first content line are ignored, so a stray empty line there no longer hides the options. *SET / *HEADING are treated as in-list heading lines: they neither end the block nor count as options. Option text is shown verbatim, as the whole line - inline commands such as 1:*? Xcar[1], 101:*PICT "_101.jpg"text or 01:A1*PROPERTIES "iProd=..." are no longer cut at the first *

4. Question Structure Sidebar (Activity Bar)

Provided by out/sidebarTreeProvider.js (logic) + Sidebar/sidebar-rules.js (editable rules).

  • A dedicated Nfield ODIN icon in the Activity Bar opens a Question Structure panel: one line per question, so the structure of a whole questionnaire is visible at a glance
  • Label format is questionNumber / variableName: the number comes from *QUESTION 123 (or *Q 123), the variable name from *VAR "xxx". When there is no *VAR, the variable name is empty and only the number is shown
  • Click a label to jump straight to that question (the line is revealed vertically centred)
  • Blocks become collapsible parents: *REPEAT/*ENDREP, *BLOCK/*ENDBLOCK and *SEGMENT/*ENDSEGMENT are shown as foldable parent nodes with the questions inside them nested underneath. Indentation is produced by the tree itself, and nested blocks (a *REPEAT inside another *REPEAT) nest correctly
  • Colours: foldable block labels are coloured differently from question labels (blocks use theme colours, questions follow the theme foreground by default); a built-in rule marks dummy questions (*DUMMY) in another colour
  • Section labels (**SECTION <name>) act as lightweight visual dividers inside long scripts: each one becomes a single flat item sitting at the same level as the questions around it, so it is folded away together with them. They are not paired — **ENDSECTION is simply ignored, and a section never swallows the questions that follow it. The name is shown verbatim, so Chinese names work too (e.g. **SECTION 第一部分_甄别). Only SECTION immediately after ** at the start of a line is recognised, so the ordinary ** comments, the ***/**** multi-level comments and the asterisk banner lines that ODIN scripts use everywhere are left alone
  • The section pattern, label text, colour and icon are editable as well: SECTION_RE, formatSectionLabel(), COLORS.section and ICONS.section in the same rules file
  • Shown for .Q / .q / .Odin / .odin files, or any file whose language mode was manually set to nfieldodin; otherwise the panel explains why it is empty
  • Refreshes automatically while editing (250 ms debounce); the refresh button in the view title bar re-reads the rules file immediately
  • All of the above is editable: Sidebar/sidebar-rules.js holds the label format (written as plain if / ternary code with obvious slots to fill in), the colours, the icons, the block definitions and the per-question colour rules. To add other blocks (e.g. *SET/*ENDSET, *MATRIX/*ENDMATRIX), copy one entry and change the regexes. No restart needed — just press refresh
  • ⚠️ Colours must be VS Code theme colour ids (charts.purple, charts.orange, ...). Tree view icons do not support #RRGGBB

5. Hover Provider

Provided by out/documentHoverProvider.js.

  • Hover over Q123 → shows question definition, question text, and all option codes
  • Hover over Q{idName} → same, matched by *ID attribute
  • Also works on *GOTO 123, *GOTO {idName}, *BACK 123, *BACK {idName}
  • Lists: hover over *USELIST "listName" or *USELIST "listName,NET=1" → shows the *LIST "listName" definition line followed by all of its options
    • Only the list name is used, so the name ends at the closing quote or the first comma — anything after the comma is a parameter and is ignored
    • The same hover also appears on the *LIST "listName" definition line itself
    • List names are matched case-insensitively, so *LIST "MyList" is found by *USELIST "mylist"
    • If no matching *LIST exists, the hover says so — handy for spotting a typo or a case mismatch
  • Display uses syntax-highlighted code block (MarkdownString with nfieldodin language)

6. Go to Definition (F12)

Provided by out/documentDefinitionProvider.js.

  • F12 on Q123 → jumps to *QUESTION 123 definition line
  • F12 on Q{idName} → jumps to the question with matching *ID attribute
  • F12 on a variable name → jumps to its *VARS, *TEXTVARS, or *SAMPLEDATA declaration

Question references open an inline Peek instead of jumping (v2.5.2+):

  • F12 on Q123 / Q{idName} → opens the Peek widget below the current line, showing the entire question block — the *QUESTION line, the question text and all of its options. Scroll inside it to proof-read, then press Esc to close. (The Peek widget is not capped at a quarter of the editor height the way hover tooltips are, so long questions are readable in one go.)
  • The revealed line sits near the top of the Peek widget (about 3 lines below its toolbar) rather than in the vertical middle, so the beginning of the question is visible right away.
  • Ctrl+Click is unchanged — it still jumps straight to the *QUESTION line.
  • F12 on a variable name is unchanged — it still jumps to its declaration.

7. Find All References (Shift+F12)

Provided by out/documentReferenceProvider.js.

  • Right-click a variable → "Find All References" or press Shift+F12
  • Supports three variable types:
    • Numeric variables (*VARS)
    • String variables (*TEXTVARS)
    • Sample data variables (*SAMPLEDATA)
  • Array variables handled: ddd[1], ddd[2] all reference ddd
  • Comma-separated definitions supported: *VARS aaa,ddd[10]
  • References inside comments (**) are automatically excluded

8. Code Snippets

Provided by snippets/nfieldodin.json. Type the prefix and press Tab.

Prefix Name Description
QSA QuestionSingle Single choice question template
QMA QuestionMulti Multiple choice question template
QOP QuestionOpen Open-ended question template
QAL QuestionAlpha Alpha (text) question template
QNU QuestionNumber Numeric question template
QMT QuestionGrid Grid/matrix question with dummy title and rows
Sub SubRoutine Subroutine template with *SUBROUTINE/*ENDSUB
rank Ranking Complete ranking question with validation logic
REP Repeat Repeat block with *REPEAT/*ENDREP and index variable
RanOrder RandomOrder Random order generator using *VARS and *REPEAT
SEG RandomSegment 9-segment random rotation with *SEGMENT/*ENDSEGMENT
STR StringGammer All string functions reference (STRLENGTH, STRSUBSTR, STRINDEX, STRREPLACE, SPLITSTRING, JOINSTRING, etc.)
GETLFQLIST GetLfqList Least-filled quota logic with *GETLFQLIST, fallback selection and extraction loop

9. Keyboard Shortcuts

All shortcuts are active only when editing Nfield ODIN files (.Q, .q, .Odin, .odin).

Text Wrapping (requires selected text)

Shortcut Command Action
Alt+1 wrapBluebTag Wrap selection with <blueb></blueb>
Alt+2 wrapRedbTag Wrap selection with <redb></redb>
Alt+3 wrapBTag Wrap selection with <b></b>

Text Transformation (requires selected text)

Shortcut Command Action
Ctrl+1 addLineNumbers Add sequential numbers with colon to each line (e.g., 1:text, 2:text)
Ctrl+2 addQuestionMark Parse selected question text and generate *QUESTION definition with numbered options
Alt+H insertHeading Generate a Net/heading *LIST with random-order scripts from selected tab-separated content
Ctrl+Shift+/ toggleBlockComment Comment out the selection and wrap it with ** ===... separator lines above and below (press again to revert)
Ctrl+0 reverseLines Reverse the order of 2 or more selected lines (line 1 becomes last, line 2 becomes second-last, etc.)

Quick Insert (at cursor position)

Shortcut Command Inserted Text
Alt+C insertControl *CONTROL Q
Alt+I insertInclude *INCLUDE Q
Alt+O insertOrder *ORDER

Script Check

Shortcut Command Action
Alt+K checkScript Audit the current script and open the result window

Question Peek (cursor on a question reference)

Shortcut Command Action
F12 peekQuestionDefinition On Q123 / Q{id}: open the inline Peek with the whole question block. Anywhere else: behave exactly like the built-in Go to Definition

10. Question Reference Formats

  • Q<number> format: e.g., Q1, Q123
  • Q{IDNAME} format: e.g., Q{satisfaction} — uses *ID attribute for lookup

11. Insert Heading (Net list)

Press Alt+H with tab-separated content selected to generate a Net heading *LIST with random-order scripts.

Input format — three tab-separated columns per line: category, option text, option code. The first row of each group carries the category in the first column; continuation rows leave the first column empty:

The command generates one *SET *ID ... *RANDOM block per category, plus Net-group randomized order scripts.

12. IntelliSense Completion

Provided by out/documentCompletionProvider.js.

  • Variable completion: suggests variables declared via *VARS, *TEXTVARS, *SAMPLEDATA (supports array suffix [n] and comma-separated lists) and question IDs from *ID "xxx" while typing
  • ODIN command keywords: 82 built-in commands (e.g., QUESTION, IF, GOTO, VARS, REPEAT, SUBROUTINE, ENDBLOCK, etc.) appear in suggestions — type * or start typing to see matching commands
  • Trigger character: *

13. Inline Formatting Tags

The grammar recognizes a set of inline tags that style their content:

Tag Effect
<blueb> Blue, bold
<redb> Red, bold
<greenb> Green, bold
<yellowb> Yellow, bold
<blue> Blue
<red> Red
<green> Green
<yellow> Yellow
<b> Bold
<i> Italic
<font ...> No colour (delimiters grey)

Tags can be nested. An inner tag keeps the style of the outer one, so a <b> inside <i> renders its text as bold and italic:

<i>Please note <b>this part</b> carefully</i>

The tag delimiters themselves (<b>, </b>, ...) are not styled — they are not affected by the italic/color of an enclosing tag.

Tags are case-insensitive: <B>…</B>, <BLUEB>…</BLUEB>, <I>…</I> and <FONT ...> highlight exactly like their lowercase forms, and the closing tag does not have to match the case of the opening one (<B>...</b> works). Only the tag names are case-insensitive — ODIN commands, built-in functions and position markers stay case-sensitive, so 1l1 and json( are still left alone.

<font ...> follows the HTML-style form and accepts any attributes, e.g. <font color='gray'>...</font>. Its content is deliberately left uncoloured (it keeps the surrounding string's colour), because the grammar does not interpret the color attribute — colouring it would suggest a colour the tag does not actually produce. <font ...> and </font> themselves use the delimiter colour, identical to <blueb> / </blueb>. The content of <font ...> is treated as final styling, so inner tags are not interpreted.

Use single quotes for attributes. A double-quoted attribute such as <font color="gray"> collides with the ODIN string delimiter " and would terminate the string early.

14. Check Script

Provided by out/scriptChecker.js (the audit logic) and out/scriptCheckPanel.js (the result window).

Run it from the editor context menu →“检查脚本”, the Command Palette (Nfield: 检查脚本), or Alt+K. The whole file is audited and the report opens in a separate webview tab beside the editor, so the source stays visible.

Prefix Meaning
1危险: Almost certainly a defect — fix it
2警告: Worth checking — may be intentional
3提醒: Informational

The three counts are summarised as pills at the top of the panel, and each prefix is colour-coded with the editor's own theme colours.

Checks include (not exhaustive):

  • question type vs. wording (*CODES / *MULTI against “单选 / 多选 / 必选 / 限选” in the question text)
  • *LIST / *USELIST option resolution and duplicated option codes
  • *INCLUDE / *CONTROL / *BACK / PROPERTY() / #Qxxx targets that do not exist, or are the wrong question type
  • *IF syntax — ;, QQ, Q1.5, non-ASCII, and & placed after \
  • tag balance for <b> / <u> / <i> / <blue> / <red> / <green> / <font> … (counted at check time — the TextMate grammar itself cannot flag unclosed tags)
  • duplicated *VAR / DIMVAR / *TABLE names, missing variable names, illegal characters
  • *TEXTVARS / *SAMPLEDATA that are declared but never assigned
  • repeated characters (叠字) as a typo hint, plus the list of questions that have no *RANDOM / *ORDER

Good to know:

  • The menu item only appears in ODIN files (when: editorLangId == nfieldodin).
  • The check reads the editor buffer, so unsaved edits are included — no need to save first.
  • The panel is reused: running the check again refreshes the same tab instead of opening a new one.
  • The result is text — Ctrl+A / Ctrl+C in the panel copies the whole report.
  • The logic was ported from the internal checking page. Browser-only parts (server callbacks, alert, showmessage) were removed; messages that used to pop up are now prepended to the report as 检查过程提示:.

Usage

Question Definition

*QUESTION 1 *CODE L5 *VAR "q1" *ID "satisfaction"
您对本次服务满意吗?
1:非常满意
2:满意
3:一般
4:不满意
5:非常不满意

Question References

Q1
Q{satisfaction}

Implementation Notes

Facts worth knowing before touching the TextMate grammar or the colour rules. Every colour below was measured by tokenising with vscode-textmate against the Dark+ theme — they are observations, not assumptions.

  1. foreground and fontStyle are resolved independently. They do not have to come from the same rule: a token takes its colour from the deepest matching rule and its font style from the nearest rule that defines one. Example — the <b> delimiter inside <i> is #808080 (from punctuation.definition.tag) × italic (from markup.italic).
  2. <b> is actually blue (#569CD6) and <i> is actually purple (#C586C0) under Dark+. The plugin sets only fontStyle for these two (no foreground), so the colour is supplied by the theme's markup.bold / markup.italic. Only the 8 coloured tags are pinned by the plugin.
  3. When no rule matches, the fallback colour is #D4D4D4 (Dark+ editor.foreground). At the TextMate layer those tokens carry colour id 0, meaning "no colour" — vscode-textmate fills slot 0 of its colour map with a hard-coded #000000, which is a placeholder rather than a rendered colour. That is why <font ...> content shows as #CE9178 inside a string (same as the surrounding text) and #D4D4D4 outside one.
  4. Position markers are not parsed inside <i>. The <i> rule includes only #tagsInItalic and #tags, so 1L1 in <i>1L1</i> stays unstyled.
  5. An unclosed <blueb> tints the rest of the document. A missing </blueb> bleeds across lines and colours everything that follows. This doubles as a useful error detector: when a large block suddenly turns blue, look for an unmatched <blueb>. A stray </blueb> is harmless.
  6. Tag names are case-insensitive, everything else is not. Every tag rule's begin/end carries the inline flag (?i) (40 occurrences across #tags and #tagsInItalic), so <BLUEB> behaves exactly like <blueb>. vscode-textmate reads no ignoreCase field — the inline flag is the only mechanism available. Commands (*IF), position markers (1L1), built-in functions and option codes stay case-sensitive by design.
  7. Position markers only highlight inside a *QUESTION / *FIELD block. The block runs from the *QUESTION / *FIELD line up to (but not including) the next line that starts with *, so question text and option lines are inside it — including 1:… *NUMBER 61L3 *SAVE Number lines under *QUESTION 1 *FORM. Outside the block a stray 1L1 stays plain; SetAnswer(1L1) is no longer coloured. A block-local string rule (#stringsInQuestion) additionally parses position markers inside quotes, which is what makes *FIELD "61L1" light up — ordinary strings are unchanged.

Tag delimiters (<blueb>, </blueb>, ...) are tagged punctuation.definition.tag.nfield by beginCaptures/endCaptures. The plugin deliberately writes no colour rule for that scope, so it falls through to the theme's punctuation.definition.tag (Dark+ = #808080). This is intentional — pinning it would override the delimiter colour on every theme.

File Structure

nfieldodin-LOCAL/
├── package.json                       # Extension manifest (commands, keybindings, language config)
├── nfieldodin-configuration.json      # Language configuration (comments, brackets, auto-closing)
├── .vsixmanifest                      # VSIX package manifest for publishing
├── icon.png                           # Extension icon
├── README.md                          # This file
├── CHANGELOG.md                       # Change log
├── LICENSE.txt                        # MIT License
├── 文件说明.md                         # Internal development notes (Chinese)
├── Sidebar/
│   └── sidebar-rules.js               # Editable rules for the Question Structure sidebar
├── syntaxes/
│   └── nfieldodin.tmLanguage.json     # TextMate grammar for syntax highlighting
├── snippets/
│   └── nfieldodin.json                # Code snippets definitions
└── out/                               # Compiled JavaScript (entry point)
    ├── extension.js                   # Main entry: registers all providers and commands
    ├── sidebarTreeProvider.js         # Question Structure sidebar (Activity Bar) provider
    ├── documentSymbolProvider.js      # Outline view provider
    ├── documentHoverProvider.js       # Hover tooltip provider
    ├── documentDefinitionProvider.js  # Go to Definition provider
    ├── documentReferenceProvider.js   # Find All References provider
    ├── documentCompletionProvider.js  # IntelliSense completion provider
    ├── variableUtils.js               # Shared variable-definition parser (used by F12 and Shift+F12)
    ├── listUtils.js                   # Shared *LIST / *USELIST parser (used by hover and outline)
    ├── questionUtils.js               # Shared question-block parser (used by hover and Go to Definition)
    ├── scriptChecker.js               # Check Script audit logic (no vscode dependency, unit-testable)
    └── scriptCheckPanel.js            # Check Script result window (webview panel)

Changelog

See CHANGELOG.md for detailed changes.


以下是中文说明 Below is the Chinese documentation


Nfield ODIN 插件

一个用于 Nfield ODIN 脚本语法高亮和辅助工具的 VS Code 插件。

关于

本插件最初由 PhoenixHuang2020 创建,现经原作者授权由 Jimmy Liu 继续维护。

邮箱:jimmy.liu@kantar.com

授权

原作者 PhoenixHuang2020 已授权 Jimmy Liu 继续维护和修改本插件。

功能说明

1. 语法高亮

由 syntaxes/nfieldodin.tmLanguage.json(TextMate 语法文件)提供。

元素 匹配模式 示例
关键字 * 开头的 ASCII 标识符 —— 首字符须为字母、_ 或 ?,因此不匹配中文、也不匹配 * + 数字(*XXX中文、(100ml*5) 里的 *5) *QUESTION、*VAR、*IF、*REPEAT
注释 ** 开头 ** 这是注释
字符串 双引号包裹 "varName"、"Hello world"
问题引用 Q + 数字 或 Q{ID名} Q123、Q{satisfaction}
选项代码 数字加冒号 1:是、02:否
位置标记 数字 + L + 数字,或 L + 数字,可再跟 . + 小数位(带词边界,仅在 *QUESTION / *FIELD 块内,跟随编辑器主题色) 1L5、L20、61L3、L3.2、64L4.2、*FIELD "61L1"
内置函数 预定义函数名(带词边界) JSON、PROPERTY、DATETIMEADD、STRLENGTH、TO
问题编号 QUESTION 后的数字 *QUESTION 123 → 123 高亮

2. 语言配置

由 nfieldodin-configuration.json 提供。

  • 行注释:** — 使用 Ctrl+/ 切换注释
  • 括号匹配:{}、[]、()
  • 自动闭合对:{}、[]、()、"、'、*REPEAT/*ENDREP、*BLOCK/*ENDBLOCK
  • 包围选择对:{}、[]、()、"、'

3. 大纲视图(文档符号)

由 out/documentSymbolProvider.js 提供。

  • 在大纲面板显示所有 *QUESTION 定义
  • 问题名称格式:Q123(varName) {idName}
    • (varName) — 来自 *VAR 属性
    • [dimvarName] — 来自 DIMVAR 属性
    • {idName} — 来自 *ID 属性
  • 虚拟问题(*DUMMY)和真实问题使用不同图标
  • 显示 *SUBROUTINE 子例程定义
  • 显示 *LIST 列表定义,名称为 List_列表名
  • 在每个问题/列表下显示选项代码(1:是)和 *USELIST 作为子项
  • *LIST 块在遇到 *ENDLIST、空行、下一个 *LIST、或行首为「* + 字母」的其它命令时结束(与悬停提示共用规则,见 out/listUtils.js)。定义行与首个内容行之间的空行会被忽略 —— 那里多写空行不再导致选项显示不出来;*SET / *HEADING 视为块内标题行,既不结束块,也不计入选项数。选项文本整行原样显示 —— 1:*? Xcar[1]、101:*PICT "_101.jpg"文本、01:A1*PROPERTIES "iProd=..." 这类行内指令不再被第一个 * 截断

4. 题目结构侧边栏(活动栏)

由 out/sidebarTreeProvider.js(解析逻辑)+ Sidebar/sidebar-rules.js(可编辑规则)提供。

  • 活动栏新增 Nfield ODIN 图标,点开后是题目结构面板:一行一个题目,整套问卷的结构一眼可见
  • 标签格式 题号 / 变量名:题号取自 *QUESTION 123(或 *Q 123),变量名取自 *VAR "xxx"。没有 *VAR 时变量名为空,只显示题号
  • 点击标签直接跳到该题(该行会显示在编辑器垂直中央)
  • 块 = 可折叠父节点:*REPEAT/*ENDREP、*BLOCK/*ENDBLOCK、*SEGMENT/*ENDSEGMENT 显示为可折叠的父节点,块内题目挂在它下面;缩进由树形结构自动产生,嵌套块(REPEAT 里再套 REPEAT)层级正确
  • 颜色:折叠标签(块)与题目标签颜色不同(块用主题色,题目默认跟随主题正文色);内置一条规则把哑题(*DUMMY)标成另一种颜色
  • 分节标签(**SECTION 名称)用来在长问卷里做「视觉分割线」:每个分节是一个平铺项,与前后题目同级,因此会跟着同级题目一起被折叠。它不需要配对 —— **ENDSECTION 会被直接忽略,分节也不会把后面的题目收成子项。显示文字就是名称原文(去掉首尾空白),所以可以直接写中文(如 **SECTION 第一部分_甄别)。只有行首 ** 紧跟 SECTION 才识别,ODIN 脚本里大量存在的行内 ** 注释、**CHANGE: 说明行、***/**** 多级注释与星号横幅都不会被误判
  • 分节的识别正则、显示文字、颜色与图标同样可改:同一个规则文件里的 SECTION_RE、formatSectionLabel()、COLORS.section、ICONS.section
  • 显示条件:.Q / .q / .Odin / .odin 文件,或手动把语言模式设成 nfieldodin;否则面板给出说明文字
  • 编辑时自动刷新(250 ms 合并);标题栏的刷新按钮会立刻重新读取规则文件
  • 以上全部可改:Sidebar/sidebar-rules.js 里放着标签格式(用 if / 三元表达式书写,位置留好了)、颜色、图标、块定义、题目颜色规则。要加别的块(如 *SET/*ENDSET、*MATRIX/*ENDMATRIX),照抄一条改正则即可。改完点刷新即生效,不需要重启
  • ⚠️ 颜色只能填 VS Code 主题色 id(charts.purple、charts.orange 等),树视图图标不支持 #RRGGBB

5. 悬停提示

由 out/documentHoverProvider.js 提供。

  • 鼠标悬停在 Q123 上 → 显示问题定义、问题文本和所有选项代码
  • 鼠标悬停在 Q{idName} 上 → 通过 *ID 属性匹配,显示相同内容
  • 同时支持 *GOTO 123、*GOTO {idName}、*BACK 123、*BACK {idName}
  • 列表:悬停在 *USELIST "listName" 或 *USELIST "listName,NET=1" 上 → 显示 *LIST "listName" 定义行,以及该列表的全部选项
    • 只使用列表名,名称在遇到 结尾双引号 或 第一个逗号 时结束 —— 逗号之后是参数,会被忽略
    • 悬停在 *LIST "listName" 定义行上也能看到同样内容
    • 列表名不区分大小写匹配,因此 *USELIST "mylist" 能找到 *LIST "MyList"
    • 若文档中不存在对应的 *LIST,悬停会直接提示 —— 便于发现拼写或大小写问题
  • 使用语法高亮的代码块显示(MarkdownString,语言为 nfieldodin)

6. 跳转到定义(F12)

由 out/documentDefinitionProvider.js 提供。

  • F12 点击 Q123 → 跳转到 *QUESTION 123 定义行
  • F12 点击 Q{idName} → 跳转到匹配 *ID 属性的问题定义
  • F12 点击变量名 → 跳转到 *VARS、*TEXTVARS 或 *SAMPLEDATA 声明位置

题目引用改为弹出内联 Peek,而不是直接跳转(v2.5.2 起):

  • 在 Q123 / Q{idName} 上按 F12 → 在当前行的下一行弹出 Peek 窗口,显示整道题的原始内容 —— *QUESTION 行、题面文本与全部选项。可在窗口内滚动核对,按 Esc 关闭。(Peek 窗口不像悬停那样被限制在「编辑器高度的 1/4」,长题目可以一次看完。)
  • 揭示的定位行会停在 Peek 窗口靠近顶部 的位置(距工具栏约 3 行),而不是垂直居中,因此题目开头一眼可见。
  • Ctrl+点击 行为不变 —— 仍然是直接跳转到 *QUESTION 行。
  • 在变量名上按 F12 行为不变 —— 仍然是跳转到声明位置。

7. 查找所有引用(Shift+F12)

由 out/documentReferenceProvider.js 提供。

  • 右键点击变量 → "查找所有引用" 或按 Shift+F12
  • 支持三种变量类型:
    • 数值变量(*VARS)
    • 字符串变量(*TEXTVARS)
    • 样本数据变量(*SAMPLEDATA)
  • 数组变量处理:ddd[1]、ddd[2] 都引用变量 ddd
  • 支持逗号分隔的多变量定义:*VARS aaa,ddd[10]
  • 注释(**)中的引用自动排除

8. 代码片段

由 snippets/nfieldodin.json 提供。输入前缀后按 Tab 展开。

前缀 名称 说明
QSA 单选题 单选题模板
QMA 多选题 多选题模板
QOP 开放题 开放式问题模板
QAL 文本题 字母文本题模板
QNU 数字题 数字题模板
QMT 矩阵题 矩阵/网格题模板,含虚拟标题和行
Sub 子例程 子例程模板,含 *SUBROUTINE/*ENDSUB
rank 排序题 完整排序题模板,含校验逻辑
REP 循环块 循环块模板,含 *REPEAT/*ENDREP 和索引变量
RanOrder 随机顺序 随机顺序生成器,使用 *VARS 和 *REPEAT
SEG 随机分段 9 段随机轮换模板,使用 *SEGMENT/*ENDSEGMENT
STR 字符串函数 全部字符串函数参考(STRLENGTH、STRSUBSTR、STRINDEX、STRREPLACE、SPLITSTRING、JOINSTRING 等)
GETLFQLIST 取小配额 取小逻辑模板,使用 *GETLFQLIST,含兜底抽取与循环抽取完整流程

9. 快捷键

所有快捷键仅在编辑 Nfield ODIN 文件时生效(.Q、.q、.Odin、.odin)。

文字包裹(需先选中文本)

快捷键 命令 功能
Alt+1 wrapBluebTag 用 <blueb></blueb> 包裹选中文字
Alt+2 wrapRedbTag 用 <redb></redb> 包裹选中文字
Alt+3 wrapBTag 用 <b></b> 包裹选中文字(加粗标记)

文本转换(需先选中文本)

快捷键 命令 功能
Ctrl+1 addLineNumbers 为每行添加序号和冒号(如 1:text、 2:text)
Ctrl+2 addQuestionMark 解析选中的问题文本,生成带编号选项的 *QUESTION 定义
Alt+H insertHeading 将选中的制表符分隔内容生成带 Net 分组的 *LIST 及随机排序脚本
Ctrl+Shift+/ toggleBlockComment 注释选中内容,并在其上下各插入一行 ** ===... 分隔符(再按一次可还原)
Ctrl+0 reverseLines 将选中的 2 行及以上内容倒序排列(第 1 行变最后一行,第 2 行变倒数第 2 行,依此类推)

快速插入(在光标位置)

快捷键 命令 插入内容
Alt+C insertControl *CONTROL Q
Alt+I insertInclude *INCLUDE Q
Alt+O insertOrder *ORDER

检查脚本

快捷键 命令 作用
Alt+K checkScript 对整份脚本做全量体检,结果在与源码并排的独立窗口里打开

题目预览(光标停在题目引用上)

快捷键 命令 作用
F12 peekQuestionDefinition 在 Q123 / Q{id} 上:弹出内联 Peek,显示整道题的内容;其它位置:与 VS Code 原生的「跳转到定义」完全一致

10. 问题引用格式

  • Q数字 格式:如 Q1、Q123
  • Q{ID名} 格式:如 Q{satisfaction} — 通过 *ID 属性查找

11. 插入标题(Net 分组列表)

选中制表符分隔的内容后按下 Alt+H,即可生成带 Net 分组的 *LIST 及随机排序脚本。

输入格式——每行三列,用制表符(Tab)分隔:分类、选项文本、选项编号。每组第一行填写分类(第一列),该组后续行第一列留空:

该命令会为每个分类生成一个 *SET *ID ... *RANDOM 分组,并附带 Net 分组的随机排序脚本。

12. 智能补全

由 out/documentCompletionProvider.js 提供。

  • 变量补全:键入时自动提示 *VARS、*TEXTVARS、*SAMPLEDATA 声明的变量名(支持数组后缀 [n] 和逗号分隔的多变量定义),以及 *ID "xxx" 中的题目 ID
  • ODIN 命令关键字补全:82 个内置命令(如 QUESTION、IF、GOTO、VARS、REPEAT、SUBROUTINE、ENDBLOCK 等)出现在提示列表中 — 输入 * 或开始键入字母即可看到匹配的命令
  • 触发字符:*

13. 内联格式标签

语法支持一组内联标签,用于设置标签内文字的样式:

标签 效果
<blueb> 蓝色加粗
<redb> 红色加粗
<greenb> 绿色加粗
<yellowb> 黄色加粗
<blue> 蓝色
<red> 红色
<green> 绿色
<yellow> 黄色
<b> 加粗
<i> 斜体
<font ...> 内容不着色(符号灰色)

标签支持嵌套。内层标签会保留外层的样式,因此 <i> 中嵌套的 <b>,其文字会同时显示为加粗 + 斜体:

<i>请注意 <b>这一部分</b> 需要填写</i>

标签符号本身(<b>、</b> 等)不参与样式渲染,不会被外层标签的斜体或颜色影响。

标签大小写不敏感:<B>…</B>、<BLUEB>…</BLUEB>、<I>…</I>、<FONT ...> 与小写写法的高亮效果完全一致;闭合标签也不要求与开标签大小写相同(<B>…</b> 同样成立)。容错范围仅限标签名 —— ODIN 命令、内置函数、位置标记仍区分大小写,1l1、json( 依旧不会被识别。

<font ...> 采用 HTML 风格的写法,可带任意属性,例如 <font color='gray'>(例如:品牌甲、品牌乙等)</font>。标签内的文字刻意不做额外着色(保持与所在字符串一致的默认颜色):语法并不解析 color 属性,若强行染成某个颜色,反而会暗示一种标签实际并不会产生的颜色。<font ...> 与 </font> 两个标签符号本身显示为分隔符颜色(与 <blueb> / </blueb> 完全一致)。<font ...> 的内容按"最终样式"处理,其内部不再解析其他标签。

属性请使用单引号。写成双引号(如 <font color="gray">)会与 ODIN 的字符串定界符 " 冲突,导致字符串被提前结束。

14. 检查脚本

由 out/scriptChecker.js(检查逻辑)与 out/scriptCheckPanel.js(结果窗口)提供。

入口:编辑器右键菜单 →「检查脚本」、命令面板(Nfield: 检查脚本)、快捷键 Alt+K。会对整份文件做一次全量体检,报告在与源码并排的独立 Webview 标签页里打开,源码保持可见。

前缀 含义
1危险: 基本可以确定是缺陷,建议修掉
2警告: 值得复查,也可能是有意为之
3提醒: 提示性信息

面板顶部用三个徽标汇总各级条数,三种前缀用编辑器的主题色上色;重复检查只刷新同一个标签页,不会堆出多个窗口。

使用示例

问题定义

*QUESTION 1 *CODE L5 *VAR "q1" *ID "satisfaction"
您对本次服务满意吗?
1:非常满意
2:满意
3:一般
4:不满意
5:非常不满意

问题引用

Q1
Q{satisfaction}

实现机制说明

以下几条是修改语法文件或配色规则前必须知道的机制。表内所有颜色都是用 vscode-textmate 真实分词、按 Dark+ 主题取色测出来的,不是推测。

  1. foreground 与 fontStyle 各自独立解析。 二者不必来自同一条规则:颜色取"最深命中的规则",字体样式取"最近一条定义了它的规则"。例:<i> 里 <b> 的标签符号 = #808080(来自 punctuation.definition.tag)× 斜体(来自 markup.italic)。
  2. <b> 实际是蓝色的(Dark+ 下 #569CD6),<i> 实际是紫色的(#C586C0)。插件对这两个标签只写了 fontStyle、没写 foreground,颜色由主题的 markup.bold / markup.italic 补上。只有 8 个彩色标签是插件固定色。
  3. 无规则命中时兜底色为 #D4D4D4(Dark+ editor.foreground)。在 TextMate 层,这类 token 的颜色编号是 0,含义为"无颜色"——vscode-textmate 把颜色表第 0 项固定填成 #000000,那只是占位符、并非实际渲染色。这就是 <font ...> 的内容在引号内显示为 #CE9178(与周围文字同色)、在引号外显示为 #D4D4D4 的原因。
  4. <i> 内部不解析位置标记。 <i> 的 patterns 只 include 了 #tagsInItalic 与 #tags,因此 <i>1L1</i> 中的 1L1 不高亮。
  5. <blueb> 缺 </blueb> 会一直染色到文档末尾。 未闭合的 <blueb> 会跨行蔓延、把后面所有文字都染成蓝色。这一点也可以反过来当作排错手段:某一大片文字突然变蓝,就去查有没有未闭合的 <blueb>。多余的 </blueb> 无害。
  6. 标签名大小写不敏感,其余仍区分大小写。 语法给每条标签规则的 begin/end 都加了内联标志 (?i)(#tags 与 #tagsInItalic 共 40 处),因此 <BLUEB> 与 <blueb> 表现完全一致。vscode-textmate 不读取任何 ignoreCase 字段,内联标志是唯一可行机制。命令(*IF)、位置标记(1L1)、内置函数、选项代码仍按设计区分大小写。
  7. 位置标记只在 *QUESTION / *FIELD 块内高亮。 块的范围 = *QUESTION / *FIELD 行起,到下一个以 * 开头的行之前,题干行与选项行都在块内(含 *QUESTION 1 *FORM 之后的 1:… *NUMBER 61L3 *SAVE Number)。块之外的 1L1 保持普通文本色 —— SetAnswer(1L1) 不再高亮。块内另用一份字符串规则(#stringsInQuestion),让引号里的位置标记也能上色,*FIELD "61L1" 因此可见;普通字符串行为不变。

标签符号(<blueb>、</blueb> 等)由 beginCaptures/endCaptures 标记为 punctuation.definition.tag.nfield。插件刻意不为其写配色规则,让它直接落回主题的 punctuation.definition.tag(Dark+ = #808080)。这是有意为之 —— 一旦写死,所有主题下所有标签符号的颜色都会被覆盖。

文件结构

nfieldodin-LOCAL/
├── package.json                       # 插件清单(命令、快捷键、语言配置)
├── nfieldodin-configuration.json      # 语言配置(注释、括号、自动闭合)
├── .vsixmanifest                      # VSIX 打包清单
├── icon.png                           # 插件图标
├── README.md                          # 本文件
├── CHANGELOG.md                       # 变更日志
├── LICENSE.txt                        # MIT 许可证
├── 文件说明.md                         # 内部开发说明
├── Sidebar/
│   └── sidebar-rules.js               # 侧边栏「题目结构」的可编辑规则
├── syntaxes/
│   └── nfieldodin.tmLanguage.json     # TextMate 语法高亮定义
├── snippets/
│   └── nfieldodin.json                # 代码片段定义
└── out/                               # 编译后的 JavaScript
    ├── extension.js                   # 主入口:注册所有功能提供者和命令
    ├── sidebarTreeProvider.js         # 侧边栏「题目结构」提供者(活动栏视图)
    ├── documentSymbolProvider.js      # 大纲视图提供者
    ├── documentHoverProvider.js       # 悬停提示提供者
    ├── documentDefinitionProvider.js  # 跳转定义提供者
    ├── documentReferenceProvider.js   # 查找引用提供者
    ├── documentCompletionProvider.js  # 智能补全提供者
    ├── variableUtils.js               # 共享的变量定义解析器(F12 与 Shift+F12 共用)
    ├── listUtils.js                   # 共享的 *LIST / *USELIST 解析器(悬停与大纲共用)
    ├── questionUtils.js               # 共享的题目块解析器(悬停与跳转定义共用)
    ├── scriptChecker.js               # 「检查脚本」的检查逻辑(不依赖 vscode,可单测)
    └── scriptCheckPanel.js            # 「检查脚本」的结果窗口(Webview 面板)

变更日志

详见 CHANGELOG.md。

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
  • Your Privacy Choices
  • Consumer Health Privacy
© 2026 Microsoft