Skip to content
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

Initial work on document formatting support for PICO-8 Lua files #27

Merged
merged 24 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b5fe987
Working on implementing code formatter
japhib Feb 25, 2022
17e3ce3
adapt previous formatter implementation to the most recent version of…
beetrootpaul Oct 16, 2022
b6a5d13
prevent formatting of ".p8" files ("pico-8" language ID) or any other…
beetrootpaul Oct 16, 2022
3aae15c
Formatter fixes: preserve comments, handle local statements without i…
japhib Oct 17, 2022
efba2f9
mention the official Language Server Extension Guide in README for an…
beetrootpaul Oct 17, 2022
4d1e788
increase readability of some test data
beetrootpaul Oct 17, 2022
818b2a8
added tests for some found errors
beetrootpaul Oct 18, 2022
22ee4a1
Fix formatting of local functions
beetrootpaul Oct 18, 2022
de21ec4
[Formatter] Add parentheses in some cases when those are needed
beetrootpaul Oct 19, 2022
68eb463
[Formatter] Add parentheses when required for a chain of same but non…
beetrootpaul Oct 23, 2022
3e406ff
Add missing charcode comments
beetrootpaul Oct 23, 2022
25a2178
Reduce ambiguity about operator being (both left and right) associative
beetrootpaul Oct 29, 2022
bb8e630
Add tests for preserving comments when formatting
beetrootpaul Oct 29, 2022
1fcbd62
Add even more tests for preserving comments, mostly by rewriting thos…
beetrootpaul Oct 29, 2022
6302aa0
Working on adding comments within expressions
japhib Oct 29, 2022
da268f0
Merge branch 'formatter' of github.com:beetrootpaul/pico8-ls into brp…
japhib Oct 29, 2022
b424cfd
Refactor Block into its own AST noe (from Statement[]) for figuring o…
japhib Oct 30, 2022
75d291f
Add more formatter tests
beetrootpaul Nov 5, 2022
1f77390
Rename Operators.isBothLeftAndRightAssociative to Operators.doesNeedP…
beetrootpaul Nov 5, 2022
4328064
Add formatter tests against changing AST structure or modifying code …
beetrootpaul Nov 8, 2022
d8bf459
Add formatter tests for blank line preservation
beetrootpaul Nov 8, 2022
0af54c8
fixed expected output of one test
beetrootpaul Nov 8, 2022
57e8e88
turn off formatting capability to make it possible to merge changes w…
beetrootpaul Nov 8, 2022
d7c324c
temporarily skip failing formatter tests
beetrootpaul Nov 8, 2022
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ such as NeoVim, Atom, etc.

- Snippets for common idioms (functions, loops, #include, etc.)
- Hover support/signature help for user-defined functions/variables
- code formatting

# Development

Please follow the official [Language Server Extension Guide](https://code.visualstudio.com/api/language-extensions/language-server-extension-guide)
to understand how to develop VS Code language server extension.

# Changelog

Expand Down
3 changes: 3 additions & 0 deletions client/src/luafmt.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module '@appguru/luafmt' {
export function formatChunk(chunk: string): string;
}
52 changes: 30 additions & 22 deletions server/src/parser/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BinaryExpression, CallExpression, Comment_, Expression, GeneralTableFie
IndexExpression, Variable, Literal, LogicalExpression, MemberExpression, StringCallExpression, StringLiteral,
TableCallExpression, TableConstructorExpression, TableKey, TableKeyString, TableValue, UnaryExpression,
} from './expressions';
import { AssignmentStatement, BreakStatement, CallStatement, Chunk, DoStatement, ElseClause, ElseifClause,
import { AssignmentStatement, Block, BreakStatement, CallStatement, Chunk, DoStatement, ElseClause, ElseifClause,
ForGenericStatement, ForNumericStatement, FunctionDeclaration, FunctionParameter, GeneralIfClause,
GotoStatement, IfClause, IfStatement, LabelStatement, LocalStatement, RepeatStatement, ReturnStatement,
Statement, WhileStatement } from './statements';
Expand All @@ -15,6 +15,13 @@ import structuredClone from '@ungap/structured-clone';
// The default AST structure is inspired by the Mozilla Parser API but can
// easily be customized by overriding these functions.
export default class AST {
static block(body: Statement[]): Block {
return {
type: 'Block',
body,
};
}

static labelStatement(label: Identifier): LabelStatement {
return {
type: 'LabelStatement',
Expand Down Expand Up @@ -42,56 +49,57 @@ export default class AST {
};
}

static ifStatement(clauses: GeneralIfClause[]): IfStatement {
static ifStatement(clauses: GeneralIfClause[], oneLine: boolean): IfStatement {
beetrootpaul marked this conversation as resolved.
Show resolved Hide resolved
return {
type: 'IfStatement',
clauses: clauses,
oneLine,
};
}

static ifClause(condition: Expression, body: Statement[]): IfClause {
static ifClause(condition: Expression, block: Block): IfClause {
return {
type: 'IfClause',
condition: condition,
body: body,
block: block,
};
}

static elseifClause(condition: Expression, body: Statement[]): ElseifClause {
static elseifClause(condition: Expression, block: Block): ElseifClause {
return {
type: 'ElseifClause',
condition: condition,
body: body,
block: block,
};
}

static elseClause(body: Statement[]): ElseClause {
static elseClause(block: Block): ElseClause {
return {
type: 'ElseClause',
body: body,
block: block,
};
}

static whileStatement(condition: Expression, body: Statement[]): WhileStatement {
static whileStatement(condition: Expression, block: Block): WhileStatement {
return {
type: 'WhileStatement',
condition: condition,
body: body,
block: block,
};
}

static doStatement(body: Statement[]): DoStatement {
static doStatement(block: Block): DoStatement {
return {
type: 'DoStatement',
body: body,
block: block,
};
}

static repeatStatement(condition: Expression, body: Statement[]): RepeatStatement {
static repeatStatement(condition: Expression, block: Block): RepeatStatement {
return {
type: 'RepeatStatement',
condition: condition,
body: body,
block: block,
};
}

Expand Down Expand Up @@ -124,14 +132,14 @@ export default class AST {
identifier: Identifier | MemberExpression | null,
parameters: FunctionParameter[],
isLocal: boolean,
body: Statement[],
block: Block,
): FunctionDeclaration {
return {
type: 'FunctionDeclaration',
identifier: identifier,
isLocal: isLocal,
parameters: parameters,
body: body,
block: block,
};
}

Expand All @@ -140,31 +148,31 @@ export default class AST {
start: Expression,
end: Expression,
step: Expression | null,
body: Statement[],
block: Block,
): ForNumericStatement {
return {
type: 'ForNumericStatement',
variable: variable,
start: start,
end: end,
step: step,
body: body,
block: block,
};
}

static forGenericStatement(variables: Identifier[], iterators: Expression[], body: Statement[]): ForGenericStatement {
static forGenericStatement(variables: Identifier[], iterators: Expression[], block: Block): ForGenericStatement {
return {
type: 'ForGenericStatement',
variables: variables,
iterators: iterators,
body: body,
block: block,
};
}

static chunk(body: Statement[], errors: ParseError[]): Chunk {
static chunk(block: Block, errors: ParseError[]): Chunk {
return {
type: 'Chunk',
body,
block,
errors,

// This stuff is added later
Expand Down
10 changes: 10 additions & 0 deletions server/src/parser/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,13 @@ export type Comment_ = ASTNode & {
value: string,
raw: string,
};

export type Whitespace = ASTNode & {
type: 'Whitespace',
// The number of newlines to insert
count: number,
};

export function isComment(node: any): node is Comment_ {
return node.type === 'Comment';
}
Loading