Skip to content

Initial lua support #1962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions data/playground/lua/lua.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
-- This is a single-line comment

--[[
This is a multi-line comment.
It spans multiple lines.
--]]

-- Variables
local a = 42
local b, c = "Hello", "World"

-- Data Types
local number = 3.14
local boolean = true
local string = "Lua is awesome!"
local table = { 1, 2, 3 }
local nilValue = nil

-- Conditional Constructs
local x = 10
local y = 20

-- if-then-else
if x < y then
print("x is less than y")
elseif x > y then
print("x is greater than y")
else
print("x is equal to y")
end

-- ternary conditional (short if-then-else)
local max = x > y and x or y
print("The maximum value is: " .. max)

-- Functions
function add(x, b)
return x + y
end

local sum = add(5, 7)
print("Sum:", sum)

-- Tables
local person = {
name = "John",
age = 30,
hobbies = { "reading", "gaming", "programming" },
address = {
street = "123 Main St",
city = "Example City",
},
}

-- String manipulation
local concatString = "Hello " .. "World"

-- Metatables and metatable operations
local mt = {
__add = function(a, b)
return a + b
end,
__sub = function(a, b)
return a - b
end,
}

setmetatable(a, mt)

-- Closures
function makeCounter()
local count = 0
return function()
count = count + 1
return count
end
end

local counter = makeCounter()

-- Coroutines
local co = coroutine.create(function()
for i = 1, 3 do
print("Coroutine", i)
coroutine.yield()
end
end)

-- Error handling
local success, result = pcall(function()
error("This is an error")
end)

if not success then
print("Error:", result)
end

-- Loop Constructs
-- while loop
local i = 1
i = 2
while i <= 5 do
print("While loop iteration: " .. i)
i = i + 1
end

-- repeat-until loop
i = 1
repeat
print("Repeat-Until loop iteration: " .. i)
i = i + 1
until i > 5

-- for loop
for j = 1, 5 do
print("For loop iteration: " .. j)
end

-- numeric for loop with step
for k = 10, 1, -1 do
print("Numeric for loop with step: " .. k)
end

-- for-in loop (iterating over a table)
local fruits = { "apple", "banana", "cherry" }
for key, value in pairs(fruits) do
print("For-In loop: " .. key .. " = " .. value)
end

-- ternary
local max = x > y and x or y
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { javaScopeSupport } from "./java";
import { javascriptScopeSupport } from "./javascript";
import { jsonScopeSupport } from "./json";
import { pythonScopeSupport } from "./python";
import { luaScopeSupport } from "./lua";
import { LanguageScopeSupportFacetMap } from "./scopeSupportFacets.types";
import { talonScopeSupport } from "./talon";
import { typescriptScopeSupport } from "./typescript";
Expand All @@ -25,6 +26,8 @@ export function getLanguageScopeSupport(
return talonScopeSupport;
case "typescript":
return typescriptScopeSupport;
case "lua":
return luaScopeSupport;
}
throw Error(`Unsupported language: '${languageId}'`);
}
21 changes: 21 additions & 0 deletions packages/common/src/scopeSupportFacets/lua.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-disable @typescript-eslint/naming-convention */

import {
LanguageScopeSupportFacetMap,
ScopeSupportFacetLevel,
} from "./scopeSupportFacets.types";

const { supported, notApplicable } = ScopeSupportFacetLevel;

export const luaScopeSupport: LanguageScopeSupportFacetMap = {
"key.attribute": notApplicable,
tags: notApplicable,
"name.assignment": supported,
"name.variable": supported,
"value.assignment": supported,
"value.variable": supported,
functionCallee: supported,
map: supported,
"branch.if": supported,
namedFunction: supported,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
languageId: lua
command:
version: 6
spokenForm: bring arg air after bat
action:
name: replaceWithTarget
source:
type: primitive
mark: {type: decoratedSymbol, symbolColor: default, character: a}
modifiers:
- type: containingScope
scopeType: {type: argumentOrParameter}
destination:
type: primitive
insertionMode: after
target:
type: primitive
mark: {type: decoratedSymbol, symbolColor: default, character: b}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
function makeCounter(a, b)
local count = 0
return function()
count = count + 1
return count
end
end
selections:
- anchor: {line: 0, character: 21}
active: {line: 0, character: 21}
marks:
default.a:
start: {line: 0, character: 21}
end: {line: 0, character: 22}
default.b:
start: {line: 0, character: 24}
end: {line: 0, character: 25}
finalState:
documentContents: |-
function makeCounter(a, b, a)
local count = 0
return function()
count = count + 1
return count
end
end
selections:
- anchor: {line: 0, character: 21}
active: {line: 0, character: 21}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
languageId: lua
command:
version: 6
spokenForm: change arg
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: argumentOrParameter}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
function add(x, b)
return x + y
end
selections:
- anchor: {line: 0, character: 13}
active: {line: 0, character: 13}
marks: {}
finalState:
documentContents: |-
function add(, b)
return x + y
end
selections:
- anchor: {line: 0, character: 13}
active: {line: 0, character: 13}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
languageId: lua
command:
version: 6
spokenForm: change arg
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: argumentOrParameter}
usePrePhraseSnapshot: true
initialState:
documentContents: |
local sum = add(5, 7)
selections:
- anchor: {line: 0, character: 16}
active: {line: 0, character: 16}
marks: {}
finalState:
documentContents: |
local sum = add(, 7)
selections:
- anchor: {line: 0, character: 16}
active: {line: 0, character: 16}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
languageId: lua
command:
version: 6
spokenForm: change call
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: functionCall}
usePrePhraseSnapshot: true
initialState:
documentContents: |
print("a is greater than 10")
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
marks: {}
finalState:
documentContents: |+

selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
languageId: lua
command:
version: 6
spokenForm: change comment
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: comment}
usePrePhraseSnapshot: true
initialState:
documentContents: |
-- This is a single-line comment
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
marks: {}
finalState:
documentContents: |+

selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
languageId: lua
command:
version: 6
spokenForm: change comment
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: comment}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
--[[
This is a multi-line comment.
It spans multiple lines.
--]]
selections:
- anchor: {line: 2, character: 4}
active: {line: 2, character: 4}
marks: {}
finalState:
documentContents: ""
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
languageId: lua
command:
version: 6
spokenForm: change condition
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: condition}
usePrePhraseSnapshot: true
initialState:
documentContents: local max = x > y and x or y
selections:
- anchor: {line: 0, character: 12}
active: {line: 0, character: 12}
marks: {}
finalState:
documentContents: local max = and x or y
selections:
- anchor: {line: 0, character: 12}
active: {line: 0, character: 12}
Loading