Skip to content

dmille56/powershell-ts-mode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

https://github.com/dmille56/powershell-ts-mode/actions/workflows/main.yml/badge.svg

About

Powershell-ts-mode is an Emacs mode for editing Powershell files using Treesitter.

https://i.imgur.com/LHOyls2.png

Table of Contents

Installation

Dependencies

  1. Emacs compiled with treesit support (version 29+)
  2. Installed powershell treesitter grammar

Config

(use-package powershell-ts-mode
  :straight (:host github :repo "dmille56/powershell-ts-mode")
  :config
  ;; Optional: if you want to disable top-level vars from being shown in imenu 
  (setq powershell-ts-enable-imenu-top-level-vars nil))

For other customizable variables see: powershell-ts-mode customize group

Grammar installation instructions

Requires: Emacs 29+, git, C compiler, C++ compiler
  1. M-x treesit-install-language-grammar
  2. Enter in powershell as your language
  3. Enter yes to build recipe for powershell interactively
  4. Enter in the url of the grammar: https://github.com/airbus-cert/tree-sitter-powershell (NOTE: tested with commit: 9d95502e730fb403bdf56279d84630c8178b10be)
  5. Stick to the defaults for git branch, “src” directory, C compiler, and C++ compilers

NOTE: for Windows I had to install GCC in order to compile the grammar.

The pre-built binaries (including tree-sitter-powershell) can be found here https://github.com/kiennq/treesit-langs/releases

Why

Powershell-mode has some bugs when dealing with strings (ex: “\home\user") and multi-line comments (ex: “<#/n###>”) that made it unusable for me. By using treesitter we’re able to get more accurate parsing and syntax highlighting rather then relying on regular expressions to do this which reduces errors.

Development cycle

  1. Spot missing syntax highlighting/indent rule
  2. View AST with treesit-explore-mode
  3. Locate the part that isn’t working
  4. Add new pattern
  5. Reload code in powershell-ts-mode.el
  6. M-x revert-buffer in your file.ps1

NOTE: there are test PowerShell files under ./test-ps-scripts/*.ps1 that can be used to ensure that the new highlighting and indent rules do not break the current expected format.