Skip to main content

sc.syntax

sc.syntax lets you add syntax highlighting to lua code to be showned in a MyGui textbox/editbox OR towards sc.ascfont!


Functions​

highlightCode​

sc.syntax.highlightCode( source, exceptionLines, theme, simpleMode )

Adds syntax highlighting to the source and returns it. You can also mark exceptionLines, The first one is where the actual error happened and the rest are code that leads to that error. If you dont want that, set it to a empty table or nil.

Arguments:

  • source [ string ] The source to highlight
  • exceptionLines [ integer[]? ] Lines where the exceptions happened
  • theme [ table? ] A custom theme. Defaults to the built-in theme.
  • simpleMode [ boolean? ] If true, the more expensive highlighting (tracking variables and functions) is disabled. Defaults to true.

Returns:

  • [ string ] The highlighted code

highlightComments​

sc.syntax.highlightComments( line, theme )

Adds syntax highlighting to a single Lua comment line. EmmyLua annotations like ---@param, ---@field, ---@class, ---@return and ---@type are highlighted too, including the names, types and descriptions that follow them. Text between backticks is highlighted as code.

Note:

Unlike highlightCode, the theme is required here.

Arguments:

  • line [ string ] The comment line to highlight
  • theme [ table ] The theme to use, same format as the one in highlightCode

Returns:

  • [ string ] The highlighted comment

Example:

local highlighted = sc.syntax.highlightComments("---@param name string The name to use", theme)

tokenize​

sc.syntax.tokenize( source )

Splits Lua source code into tokens. Whitespace, newlines and comments are included, so you can rebuild the source from the tokens.

Arguments:

  • source [ string ] The Lua source code to tokenize

Returns:

  • [ table[] ] A list of tokens. Each token has these fields:
    • type [ string ] The kind of token, for example "KEYWORD", "IDENTIFIER", "STRING", "NUMBER" or "COMMENT"
    • value [ string ] The text of the token
    • line [ integer ] The line the token is on

Example:

for _, token in ipairs(sc.syntax.tokenize("local x = 1")) do
print(token.type, token.value)
end