-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathindex.ts
More file actions
97 lines (87 loc) · 2.71 KB
/
index.ts
File metadata and controls
97 lines (87 loc) · 2.71 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
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 { isComment, isBlockComment } from './slang-utils/is-comment.js';
import { locEnd, locStart } from './slang-utils/loc.js';
import type {
Parser,
Printer,
RequiredOptions,
SupportLanguage
} from 'prettier';
import type { AstNode } 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<AstNode> = {
astFormat: slangAstId,
parse: slangParse,
locStart,
locEnd
};
const parsers = {
[slangParserId]: slangParser,
[antlrParserId]: antlrParser
};
const antlrCanAttachComment = (node: { type: string }): boolean =>
typeof node.type === 'string' &&
node.type !== 'BlockComment' &&
node.type !== 'LineComment';
const canAttachComment = (node: AstNode): boolean =>
typeof node !== 'string' &&
typeof node !== 'undefined' &&
node.kind && // Make sure it's not Location
!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<AstNode> = {
canAttachComment,
handleComments,
isBlockComment,
massageAstNode,
print: slangPrint,
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 };