PhilippeChab / nwscript-ee-language-server

A NWScript Language Server.
https://marketplace.visualstudio.com/items?itemName=PhilippeChab.nwscript-ee-language-server
MIT License
20 stars 7 forks source link
language lsp-server neverwinter-nights neverwinter-nights-enhanced-edition vscode vscode-extension

NWScript: EE Language Server

Build Tests

NWScript: EE Language Server is a Visual Studio Code extension for the NWScript language.

Features

Dependencies

Formatting

clang-format.

Diagnostics

Neverwinter Nights home and installation folders.

Usage

Simply open a project with nss files and the extension installed.

Formatting

{
  "editor.formatOnSave": true,
  "files.associations": {
    "*.nss": "nwscript"
  },
  "[nwscript]": {
    "editor.defaultFormatter": "PhilippeChab.nwscript-ee-language-server"
  },
  "nwscript-ee-lsp.formatter": {
    "enabled": true,
    "executable": "clang-format",
    "ignoredGlobs": ["/folder/to/ignore/*.nss", "file/to/ignore/filename.nss"],
    "style": {
      "BasedOnStyle": "Google",
      "AlignTrailingComments": true,
      "AlignConsecutiveAssignments": true,
      "ColumnLimit": 250,
      "BreakBeforeBraces": "Allman",
      "AlignEscapedNewlinesLeft": true,
      "AlwaysBreakBeforeMultilineStrings": true,
      "MaxEmptyLinesToKeep": 1,
      "TabWidth": 4,
      "IndentWidth": 4,
      "UseTab": "Always"
    }
  }
}

Notes:

Diagnostics

{
  "nwscript-ee-lsp.compiler": {
    "enabled": true,
    "verbose": false,
    "nwnHome": "C:\\Users\\YOUR_USERNAME\\Documents\\Neverwinter Nights",
    "nwnInstallation": "D:\\Program Files (x86)\\Steam\\steamapps\\common\\Neverwinter Nights"
  }
}

Notes:

Syntax highligthing

I personally use the One Dark Pro theme . See VS Code documentation if you wish to customize the highlighting further.

Building and running

Generating the language library definitions

Replace /server/scripts/nwscript.nss by its new version, /server/scripts/base_scripts/ files by their new versions, /server/scripts/ovr/ includes by their new versions and execute yarn run generate-lib-defs in the server root directory.

Notes

The language server symbols or tokens are not generated using an AST like language servers usually do. The NWScript Language Server exploits its TextMate grammar, which is derived from C's, to transform a file of code into tokens. While it works well for most cases since it is a simple scripting language built on C - even for a language like NWScript, we need to cheat and use lookahead and lookbehind strategies to ensure we are in the right context -, it will also fail for complex or uncommon code structures and styles. A TextMate grammar will never cover the most extreme cases of a language grammar. An AST represents the hierarchical structure of a file of code in a much more complete and precise way.

Implementing a language lexer and parser to build its AST is a lot of work, and none was available at the time I implemented this project. Now that NWScript compiler has been made public, it would be much easier to create a utility responsible for parsing a file of code and generating its AST. Implementing this utility and refactoring the whole tokenization engine of the Language Server is, however, a non-negligible amount of work. Considering the fact that the current solution works well for common use, I do not intend to do it.

Known issues

The nwnsc process doesn't terminate on linux. This is caused by the compiler itself, not the extension.

Issues

Please report any issues on the github repository.