-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathindex.ts
More file actions
105 lines (95 loc) · 3.03 KB
/
index.ts
File metadata and controls
105 lines (95 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import * as comments from './comments/index.js';
import { handleComments, printComment } from './slang-comments/index.js';
import massageAstNode from './clean.js';
import loc from './loc.js';
import options from './options.js';
import antlrParse from './parser.js';
import antlrPrint from './printer.js';
import slangParse from './slangSolidityParser.js';
import slangPrint from './slangPrinter.js';
import { isBlockComment, isComment } from './slang-utils/is-comment.js';
import { locEnd, locStart } from './slang-utils/loc.js';
import { hasPrettierIgnore } from './slang-utils/has-prettier-ignore.js';
import { getVisitorKeys } from './slang-utils/get-visitor-keys.js';
import type {
AstPath,
Doc,
Parser,
ParserOptions,
Printer,
RequiredOptions,
SupportLanguage
} from 'prettier';
import type { PrintableNode } from './slang-nodes/types.d.ts';
const slangParserId = 'slang';
const antlrParserId = 'antlr';
const slangAstId = 'slang-ast';
const antlrAstId = 'antlr-ast';
// https://prettier.io/docs/en/plugins.html#languages
// https://github.com/github-linguist/linguist/blob/master/lib/linguist/languages.yml
const languages: SupportLanguage[] = [
{
linguistLanguageId: 237469032,
name: 'Solidity',
aceMode: 'text',
tmScope: 'source.solidity',
extensions: ['.sol'],
parsers: [slangParserId, antlrParserId],
vscodeLanguageIds: ['solidity']
}
];
// https://prettier.io/docs/en/plugins.html#parsers
const antlrParser = { astFormat: antlrAstId, parse: antlrParse, ...loc };
const slangParser: Parser<PrintableNode> = {
astFormat: slangAstId,
parse: slangParse,
locStart,
locEnd
};
const parsers = {
[slangParserId]: slangParser,
[antlrParserId]: antlrParser
};
const antlrCanAttachComment = ({ type }: { type: string }): boolean =>
typeof type === 'string' && type !== 'BlockComment' && type !== 'LineComment';
const canAttachComment = (node: PrintableNode): boolean =>
// Make sure it's not Location
node.kind && !isComment(node);
// https://prettier.io/docs/en/plugins.html#printers
const antlrPrinter = {
canAttachComment: antlrCanAttachComment,
handleComments: {
ownLine: comments.solidityHandleOwnLineComment,
endOfLine: comments.solidityHandleEndOfLineComment,
remaining: comments.solidityHandleRemainingComment
},
isBlockComment: comments.isBlockComment,
massageAstNode,
print: antlrPrint,
printComment: comments.printComment
};
const slangPrinter: Printer<PrintableNode> = {
canAttachComment,
handleComments,
isBlockComment,
massageAstNode,
print: slangPrint as (
path: AstPath<PrintableNode>,
options: ParserOptions<PrintableNode>,
print: (path: AstPath<PrintableNode>) => Doc,
args?: unknown
) => Doc,
hasPrettierIgnore,
getVisitorKeys,
printComment
};
const printers = {
[slangAstId]: slangPrinter,
[antlrAstId]: antlrPrinter
};
// https://prettier.io/docs/en/plugins.html#defaultoptions
const defaultOptions: Partial<RequiredOptions> = {
bracketSpacing: false,
tabWidth: 4
};
export default { languages, parsers, printers, options, defaultOptions };