FHIR / vscode-fsh

A Visual Studio Code language extension for FHIR Shorthand (FSH)
Apache License 2.0
18 stars 3 forks source link

FSH Language Support for VS Code

A language support extension for the FHIR Shorthand (FSH) language.

FHIR Foundation Project Statement

How to Download

In Visual Studio Code, go to the VS Code Extension Marketplace and download the vscode-fsh extension. Once activated, this extension's features should be automatically implemented.

Language Features

Syntax Highlighting

FSH Syntax

FSH files automatically have syntax highlighting applied. This allows for easier reading and writing of FHIR Shorthand.

Snippets

FSH Snippets

FSH Snippets make creating new FSH items a breeze! Snippets automatically add relevant keywords and placeholders so you can easily enter all of the recommended metadata for a FSH definition. Snippets will even auto-create your Id and Title for you based on the name.

To use snippets, type one of the trigger phrases, hit the <TAB> or <ENTER> key, type in the first field value, and hit the <TAB> key to move to the next placeholder. All FSH snippets always stop at the first rule in the definition.

Trigger FSH Item Keywords
pro Profile Profile, Parent, Id (auto), Title (auto), Description
ext Extension Extension, Id (auto), Title (auto), Description
log Logical Logical, Parent, Id (auto), Title (auto), Description
res Resource Resource, Parent (choice), Id (auto), Title (auto), Description
vs ValueSet ValueSet, Id (auto), Title (auto), Description
cs CodeSystem CodeSystem, Id (auto), Title (auto), Description
inst Instance Instance, InstanceOf, Usage (choice), Title (auto), Description
inv Invariant Invariant, Description, Expression, Severity (choice), XPath
map Mapping Mapping, Source, Target, Id, Title (auto), Description
rs RuleSet RuleSet

Additionally, a snippet is provided to help when writing slicing rules. When the start of the phrase ^slicing is detected, a block of rules can be inserted for the paths commonly set when defining slicing on an element.

Enhanced Autocomplete

FSH entity definitions have their names provided as autocomplete results in contextually appropriate scenarios.

When writing rules that apply to an element, element paths will be suggested. This feature is still undergoing development, so there are some known limitations to this feature:

Go to Definition

FSH Go to Definition

FSH entity definitions within your workspace can be found from anywhere their name is used. To go to the entity definition, right-click on the entity name and select "Go to Definition" from the context menu. Or, you can press <F12> when your text cursor is on the entity name.

Open FHIR Documentation

FSH Go to Definition

Documentation pages can be opened directly from your FSH files. Right-click on the name of any FHIR resource or FSH keyword, and select "Open FHIR Documentation" from the context menu.

Tasks

FSH Run SUSHI Task

The extension provides a custom task for running SUSHI on the current workspace. The task will run SUSHI on the workspace, log messages to VS Code's integrated Terminal tab, report any errors or warnings in VS Code's Problems tab, and highlight errors and warnings inline in the FSH file. Selecting an error or warning from the Problems tab will open the file the error is in.

Note that after any errors or warnings are resolved in the FSH, SUSHI must be run through the task to resolve them in the Problems tab and inline in the FSH file.

To run the SUSHI Build task, use VS Code's Run Task feature. The "Run Task" feature can be accessed in the "Terminal" menu by clicking "Run Task...". When the menu opens in VS Code, select 'fsh' and then select 'sushi'. The task can also be run using the keyboard shortcut for running build tasks, which is ⇧⌘B on Mac and Ctrl+Shift+B on Windows.

Note that you must have SUSHI installed locally in order for the task to run successfully. See SUSHI Installation instructions for help installing.

Instructions for Developers

Intro to FSH VS Code Extension Development

To learn more about the FSH VS Code Extension, watch the Knowledge Sharing Sessions for Developing FSH Tools (view the slides here) and Developing FSH VS Code Extension (view the slides here). These sessions provide a technical overview of the codebase and summarize key concepts for developers.

Compile and Run

NPM Tasks

The following NPM tasks may be useful in development:

Task Description
build compiles src/**/*.ts files to out/**/*.js files using the TypeScript compiler (tsc)
build:watch similar to build but automatically builds when changes are detected in src files
lint checks all src files to ensure they follow project code styles and rules
lint:fix fixes lint errors when automatic fixes are available for them
prettier checks all src files to ensure they follow project formatting conventions
prettier:fix fixes prettier errors by rewriting files using project formatting conventions
test runs the test suite
check runs all the checks performed as part of ci (lint, prettier, test)

To run any of these tasks, use npm run. For example:

$ npm run build:watch

Note about testing

The test task will often (but not always) produce an error if it is run while VSCode is open. If you receive this error, close VSCode and run the task from the command line.

Updating the Grammar

The src/lang/ directory contains the FSH lexer and parser. When the FSH grammar changes, the files in this directory will need to be updated. These files are generated in the SUSHI project by running ANTLR on the grammar definition files. The files are then copied from the SUSHI project to this project. The files to copy are:

License

Copyright 2019-2024 Health Level Seven International

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.