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

Start the development of a plugin for Prettier #348

Merged
merged 1 commit into from
Sep 25, 2020
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
1 change: 1 addition & 0 deletions tools/prettier-plugin-cadence/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
19 changes: 19 additions & 0 deletions tools/prettier-plugin-cadence/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Cadence Plugin for Prettier

Current status: 🚧 Under construction, not ready for use yet

## Development

### Getting Started

* Compile the Cadence parser tool: `go build ../../runtime/cmd/parse`
* Install the dependencies: `npm install`
* Compile the plugin: `npx tsc`
* Try the plugin on a test file: `npx prettier test.cdc --plugin=.`

### Notes

- The Prettier Playground is very useful for determining what documents to generate,
e.g. https://prettier.io/playground/#N4Igxg9gdgLgprEAuEAdKAzArlMMCW0ABAIYAUYSRUWAtgEZwBOANEQCZUDOMT+UAcwCUVfjCLB0RIvXQBfdCBYgIABwLQuyUCSZMIAdwAKuhFpQkANgZIBPLcvpMSYANZwYAZVUv+A5LxYcMoAFjC0lgDqIfjwXD5gcJ5msfgAbrG2yOBcDiD8XMwwRs4CtCTIGFaFygBWXAAeAELObh6eJLRwADL8cJXVwSD1DZ5+lnAAilgQ8AOWNSA+TIVM2TC2qnBcYHzqSkt8sJH47DAhyAAcAAzKqvqFkc6q2ffbzGn9ygCOM-AlanMIBIXAAtFA4HB2FCDkw4L98HCSiQyhUkFUFkNCrR8PNFlxxlM-v10YNlDASPQTmcLkgACzk5z4Sx+ADCEFo5Wy2wArAcsIUACqU8wYxZpIIASSg0Ngnl2+HUAEEZZ4NhM8XA5HIgA


182 changes: 182 additions & 0 deletions tools/prettier-plugin-cadence/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tools/prettier-plugin-cadence/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "prettier-plugin-cadence",
"version": "0.1.0",
"description": "",
"main": "dist",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "Apache-2.0",
"devDependencies": {
"@types/execa": "^2.0.0",
"@types/node": "^14.0.27",
"@types/prettier": "^2.0.2",
"prettier": "^2.0.5",
"typescript": "^3.9.7"
},
"dependencies": {
"execa": "^4.0.3"
}
}
32 changes: 32 additions & 0 deletions tools/prettier-plugin-cadence/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {parse} from "./parse";
import {print} from "./print";

export const languages = [
{
name: "Cadence",
parsers: ["cadence"],
since: "1.0.0",
extensions: [".cdc"],
tmScope: "source.cadence",
vscodeLanguageIds: ["cadence"],
},
];

export const parsers = {
cadence: {
parse,
astFormat: "cadence",
locStart(node: any) {
return node.StartPos.Offset;
},
locEnd(node: any) {
return node.EndPos.Offset;
},
},
};

export const printers = {
"cadence": {
print,
},
};
39 changes: 39 additions & 0 deletions tools/prettier-plugin-cadence/src/nodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

export type Node =
| Program
| Declaration
| Expression

export interface Identifier {
Identifier: string
}

export interface Program {
Type: "Program"
Declarations: Declaration[]
}

// Declarations

export type Declaration =
FunctionDeclaration

export interface FunctionDeclaration {
Type: "FunctionDeclaration"
Identifier: Identifier
}

// Expressions

export type Expression =
StringExpression
| NilExpression

export interface NilExpression {
Type: "NilExpression"
}

interface StringExpression {
Type: "StringExpression"
Value: string
}
15 changes: 15 additions & 0 deletions tools/prettier-plugin-cadence/src/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {AST, Parser, ParserOptions} from "prettier";
import * as execa from "execa"

export function parse(
text: string,
parsers: { [parserName: string]: Parser },
opts: ParserOptions,
): AST | null {
const returnValue = execa.sync("./parse", ['--json'], {input: text});
const result = JSON.parse(returnValue.stdout)[0]
if (result.Error) {
return null
}
return result.Program
}
45 changes: 45 additions & 0 deletions tools/prettier-plugin-cadence/src/print.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {doc, Doc, FastPath} from "prettier";
import {Node} from "./nodes";
import concat = doc.builders.concat;
import hardline = doc.builders.hardline;
import join = doc.builders.join;
import group = doc.builders.group;

export function print(
path: FastPath<Node>,
options: object,
print: (path: FastPath) => Doc
): Doc | null {
const n = path.getValue();
switch (n.Type) {
case "StringExpression":
return concat(['"', n.Value, '"'])

case "NilExpression":
return "nil"

case "Program":
const parts = path.map(print, 'Declarations')

// Only force a trailing newline if there were any contents.
if (n.Declarations.length) {
parts.push(hardline);
}

return join(hardline, parts)

case "FunctionDeclaration":
return concat([
"fun ",
n.Identifier.Identifier,
group(concat(['(', ')'])),
" ",
"{",
"}"
])

default:
const some: {Type: string} = n
throw new Error(`unsupported AST node: ${some.Type}`)
}
}
1 change: 1 addition & 0 deletions tools/prettier-plugin-cadence/test.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fun hello() { }
14 changes: 14 additions & 0 deletions tools/prettier-plugin-cadence/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

{
"compilerOptions": {
"esModuleInterop": true,
"inlineSourceMap": true,
"lib": ["es2016"],
"moduleResolution": "node",
"outDir": "dist",
"rootDir": "src",
"strict": true
},
"include": ["src/**/*"]
}