LuaLS / lua-language-server

A language server that offers Lua language support - programmed in Lua
https://luals.github.io
MIT License
3.38k stars 319 forks source link

`@language` tag for embedding other languages in a string #2925

Open justarandomgeek opened 3 weeks ago

justarandomgeek commented 3 weeks ago

In a few places in factorio's API we have a lua string that contains code (sometimes more lua code, sometimes our noise-expression language), and it woudl be nice to be able to activate language features for these via embedded language tags, something like

local simulation = {
  ---@language lua
  init = [[
    local bp="0eNq1lVtugzAQRfcy36aKzZu/dBtVhIA47UhgkDFRoogFdCHdWFdSG9QEKYRH1fwgbMbncsejmQukecMriUJBdAHMSlFD9HaBGt9Fkps9da44RICKF0BAJIVZCTwht1STcmgJoNjzE0S03RHgQqFC3lO6xTkWTZFyqQPGzhOoylofKYVR0xjLeXEJnPULa1tyB2HLIO4kxF4GYZMQZxnEnoS4yyCbG4SAviUlyzxO+UdyxFKaoAxl1qCK9bf99eQBZa3iu7s8olSN3rlK9xHWFnp4rRJTDxajju8EtucEZruoEpkoowbfn1/Q9rGCZ0atNnhqHpLvh3ePekXtdteOmfeWmaeTGfRHIFZdJHk+VVs6oWOwYA3MnoGFa2BsBkY3a2h0jkbX0DZD2vPq7/VZ9cce1B+9NZNfWUsLpSg6obtEsPk8HDBXXD5oojPGG+OaMttxPT8Ih311hVf6yKv9R6/0f71u77z+zajb+dQTp5tM0WCQETjqv+qcsECXUMh8FgaB5+v28QOiY0wL"
    game.tick_paused = false
    game.simulation.camera_alt_info = true
    local result = {game.surfaces[1].create_entities_from_blueprint_string
    {
      string = bp,
      position = {0, 0},
    }}
    remote.call("nixie-tubes", "RebuildNixies")
  ]],
}
local noise = {
    type = "noise-expression",
    name = "enemy_base_frequency",
    ---@language factorio-noise
    expression = "(0.00001 + 0.000003 * enemy_base_intensity) * var('control:enemy-base:frequency')"
  }
CppCXY commented 2 weeks ago

VSCode does not have native language-inject functionality, implementing these is very difficult.

CppCXY commented 2 weeks ago

I have observed the implementation here, and there is indeed feasibility. However, it requires intercepting LSP requests and forwarding them to other language processors. The language server needs to determine which language server to forward the completion request to, which may significantly slow down the completion performance of LuaLS. Otherwise, the only option is to judge through the VSCode frontend, but the parsers available on npm are generally poor and do not support analysis of ---@language."