Skip to content

Commit ab19ed8

Browse files
committed
C API: rename parser -> document
I want to allow our VS Code extension to communicate configuration file changes to quick-lint-js. To make this possible, we need most of the features of qljs_vscode_parser, but not linting. Make qljs_vscode_parser feel more general-purpose by renaming it to qljs_vscode_document. This commit should not change behavior.
1 parent dcf33a0 commit ab19ed8

File tree

10 files changed

+82
-80
lines changed

10 files changed

+82
-80
lines changed

plugin/vscode/extension.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@ let {
1515
let processFactoryPromise = createProcessFactoryAsync();
1616

1717
let DocumentLinterState = {
18-
// A Parser hasn't been created yet.
18+
// A DocumentForVSCode hasn't been created yet.
1919
NO_PARSER: "NO_PARSER",
2020

21-
// A Parser is in the process of being created.
21+
// A DocumentForVSCode is in the process of being created.
2222
CREATING_PARSER: "CREATING_PARSER",
2323

24-
// A Parser has been created, but it has no text.
24+
// A DocumentForVSCode has been created, but it has no text.
2525
PARSER_UNINITIALIZED: "PARSER_UNINITIALIZED",
2626

27-
// A Parser has been created, and its text is up-to-date with the
27+
// A DocumentForVSCode has been created, and its text is up-to-date with the
2828
// vscode.Document.
2929
PARSER_LOADED: "PARSER_LOADED",
3030

31-
// The Parser's Process crashed, and we are creating a new Process and Parser.
31+
// The DocumentForVSCode's Process crashed, and we are creating a new Process
32+
// and DocumentForVSCode.
3233
//
3334
// Document changes should be queued.
3435
RECOVERING: "RECOVERING",
@@ -73,7 +74,7 @@ class DocumentLinter {
7374
let factory = await processFactoryPromise;
7475
// TODO(strager): Reuse processes across documents.
7576
let process = await factory.createProcessAsync();
76-
let parser = await process.createParserForVSCodeAsync();
77+
let parser = await process.createDocumentForVSCodeAsync();
7778

7879
if (this._state === DocumentLinterState.DISPOSED) {
7980
parser.dispose();
@@ -251,7 +252,7 @@ class DocumentLinter {
251252
// TODO(strager): Reuse processes across documents.
252253
let factory = await processFactoryPromise;
253254
let process = await factory.createProcessAsync();
254-
let parser = await process.createParserForVSCodeAsync();
255+
let parser = await process.createDocumentForVSCodeAsync();
255256

256257
// BEGIN CRITICAL SECTION (no awaiting below)
257258
assert.strictEqual(this._state, DocumentLinterState.RECOVERING);

plugin/vscode/test/other-tests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ let tests = {
266266
let rng = new ExhaustiveRNG();
267267
injectFaults(
268268
(functionName) => {
269-
// TODO(strager): Figure out why qljs_vscode_create_parser failures
269+
// TODO(strager): Figure out why qljs_vscode_create_document failures
270270
// cause this test to fail.
271-
if (functionName !== "qljs_vscode_create_parser") {
271+
if (functionName !== "qljs_vscode_create_document") {
272272
let shouldCrash = rng.nextCoinFlip();
273273
coinFlips.push(shouldCrash);
274274
if (shouldCrash) {

src/c-api.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
namespace quick_lint_js {
1818
namespace {
1919
template <class Locator, class ErrorReporter>
20-
class qljs_parser_base {
20+
class qljs_document_base {
2121
public:
2222
const auto* lint() {
2323
this->error_reporter_.reset();
@@ -39,8 +39,8 @@ class qljs_parser_base {
3939
}
4040
}
4141

42-
struct qljs_vscode_parser final
43-
: public quick_lint_js::qljs_parser_base<
42+
struct qljs_vscode_document final
43+
: public quick_lint_js::qljs_document_base<
4444
quick_lint_js::lsp_locator,
4545
quick_lint_js::c_api_error_reporter<qljs_vscode_diagnostic,
4646
quick_lint_js::lsp_locator>> {
@@ -59,14 +59,14 @@ struct qljs_vscode_parser final
5959
}
6060
};
6161

62-
qljs_vscode_parser* qljs_vscode_create_parser(void) {
63-
qljs_vscode_parser* p = new qljs_vscode_parser();
62+
qljs_vscode_document* qljs_vscode_create_document(void) {
63+
qljs_vscode_document* p = new qljs_vscode_document();
6464
return p;
6565
}
6666

67-
void qljs_vscode_destroy_parser(qljs_vscode_parser* p) { delete p; }
67+
void qljs_vscode_destroy_document(qljs_vscode_document* p) { delete p; }
6868

69-
void qljs_vscode_replace_text(qljs_vscode_parser* p, int start_line,
69+
void qljs_vscode_replace_text(qljs_vscode_document* p, int start_line,
7070
int start_character, int end_line,
7171
int end_character,
7272
const void* replacement_text_utf_8,
@@ -78,12 +78,12 @@ void qljs_vscode_replace_text(qljs_vscode_parser* p, int start_line,
7878
replacement_text_byte_count));
7979
}
8080

81-
const qljs_vscode_diagnostic* qljs_vscode_lint(qljs_vscode_parser* p) {
81+
const qljs_vscode_diagnostic* qljs_vscode_lint(qljs_vscode_document* p) {
8282
return p->lint();
8383
}
8484

85-
struct qljs_web_demo_parser final
86-
: public quick_lint_js::qljs_parser_base<
85+
struct qljs_web_demo_document final
86+
: public quick_lint_js::qljs_document_base<
8787
quick_lint_js::web_demo_locator,
8888
quick_lint_js::c_api_error_reporter<
8989
qljs_web_demo_diagnostic, quick_lint_js::web_demo_locator>> {
@@ -93,21 +93,21 @@ struct qljs_web_demo_parser final
9393
}
9494
};
9595

96-
qljs_web_demo_parser* qljs_web_demo_create_parser(void) {
97-
qljs_web_demo_parser* p = new qljs_web_demo_parser();
96+
qljs_web_demo_document* qljs_web_demo_create_document(void) {
97+
qljs_web_demo_document* p = new qljs_web_demo_document();
9898
return p;
9999
}
100100

101-
void qljs_web_demo_destroy_parser(qljs_web_demo_parser* p) { delete p; }
101+
void qljs_web_demo_destroy_document(qljs_web_demo_document* p) { delete p; }
102102

103-
void qljs_web_demo_set_text(qljs_web_demo_parser* p, const void* text_utf_8,
103+
void qljs_web_demo_set_text(qljs_web_demo_document* p, const void* text_utf_8,
104104
size_t text_byte_count) {
105105
p->set_text(quick_lint_js::string8_view(
106106
reinterpret_cast<const quick_lint_js::char8*>(text_utf_8),
107107
text_byte_count));
108108
}
109109

110-
const qljs_web_demo_diagnostic* qljs_web_demo_lint(qljs_web_demo_parser* p) {
110+
const qljs_web_demo_diagnostic* qljs_web_demo_lint(qljs_web_demo_document* p) {
111111
return p->lint();
112112
}
113113

src/quick-lint-js/c-api.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ typedef enum qljs_severity {
1515
qljs_severity_warning = 2,
1616
} qljs_severity;
1717

18-
typedef struct qljs_vscode_parser qljs_vscode_parser;
18+
typedef struct qljs_vscode_document qljs_vscode_document;
1919
struct qljs_vscode_diagnostic {
2020
const char* message;
2121
const char* code;
@@ -25,16 +25,16 @@ struct qljs_vscode_diagnostic {
2525
int end_line;
2626
int end_character;
2727
};
28-
qljs_vscode_parser* qljs_vscode_create_parser(void);
29-
void qljs_vscode_destroy_parser(qljs_vscode_parser*);
30-
void qljs_vscode_replace_text(qljs_vscode_parser*, int start_line,
28+
qljs_vscode_document* qljs_vscode_create_document(void);
29+
void qljs_vscode_destroy_document(qljs_vscode_document*);
30+
void qljs_vscode_replace_text(qljs_vscode_document*, int start_line,
3131
int start_character, int end_line,
3232
int end_character,
3333
const void* replacement_text_utf_8,
3434
size_t replacement_text_byte_count);
35-
const qljs_vscode_diagnostic* qljs_vscode_lint(qljs_vscode_parser*);
35+
const qljs_vscode_diagnostic* qljs_vscode_lint(qljs_vscode_document*);
3636

37-
typedef struct qljs_web_demo_parser qljs_web_demo_parser;
37+
typedef struct qljs_web_demo_document qljs_web_demo_document;
3838
struct qljs_web_demo_diagnostic {
3939
const char* message;
4040
const char* code;
@@ -43,11 +43,11 @@ struct qljs_web_demo_diagnostic {
4343
int begin_offset;
4444
int end_offset;
4545
};
46-
qljs_web_demo_parser* qljs_web_demo_create_parser(void);
47-
void qljs_web_demo_destroy_parser(qljs_web_demo_parser*);
48-
void qljs_web_demo_set_text(qljs_web_demo_parser*, const void* text_utf_8,
46+
qljs_web_demo_document* qljs_web_demo_create_document(void);
47+
void qljs_web_demo_destroy_document(qljs_web_demo_document*);
48+
void qljs_web_demo_set_text(qljs_web_demo_document*, const void* text_utf_8,
4949
size_t text_byte_count);
50-
const qljs_web_demo_diagnostic* qljs_web_demo_lint(qljs_web_demo_parser*);
50+
const qljs_web_demo_diagnostic* qljs_web_demo_lint(qljs_web_demo_document*);
5151

5252
#if defined(__cplusplus)
5353
}

test/test-c-api.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
namespace quick_lint_js {
99
namespace {
1010
TEST(test_c_api_vscode, empty_document_has_no_diagnostics) {
11-
qljs_vscode_parser* p = qljs_vscode_create_parser();
11+
qljs_vscode_document* p = qljs_vscode_create_document();
1212
const qljs_vscode_diagnostic* diagnostics = qljs_vscode_lint(p);
1313
EXPECT_EQ(diagnostics[0].message, nullptr);
14-
qljs_vscode_destroy_parser(p);
14+
qljs_vscode_destroy_document(p);
1515
}
1616

1717
TEST(test_c_api_web_demo, empty_document_has_no_diagnostics) {
18-
qljs_web_demo_parser* p = qljs_web_demo_create_parser();
18+
qljs_web_demo_document* p = qljs_web_demo_create_document();
1919
const qljs_web_demo_diagnostic* diagnostics = qljs_web_demo_lint(p);
2020
EXPECT_EQ(diagnostics[0].message, nullptr);
21-
qljs_web_demo_destroy_parser(p);
21+
qljs_web_demo_destroy_document(p);
2222
}
2323

2424
TEST(test_c_api_vscode, lint_error_after_text_insertion) {
25-
qljs_vscode_parser* p = qljs_vscode_create_parser();
25+
qljs_vscode_document* p = qljs_vscode_create_document();
2626

2727
const char8* document_text = u8"let x;let x;";
2828
qljs_vscode_replace_text(p, /*start_line=*/0, /*start_character=*/0,
@@ -40,11 +40,11 @@ TEST(test_c_api_vscode, lint_error_after_text_insertion) {
4040
EXPECT_EQ(diagnostics[0].end_line, 0);
4141
EXPECT_EQ(diagnostics[0].end_character, strlen(u8"let x;let x"));
4242

43-
qljs_vscode_destroy_parser(p);
43+
qljs_vscode_destroy_document(p);
4444
}
4545

4646
TEST(test_c_api_web_demo, lint_error_after_text_insertion) {
47-
qljs_web_demo_parser* p = qljs_web_demo_create_parser();
47+
qljs_web_demo_document* p = qljs_web_demo_create_document();
4848

4949
const char8* document_text = u8"let x;let x;";
5050
qljs_web_demo_set_text(p, document_text, strlen(document_text));
@@ -58,11 +58,11 @@ TEST(test_c_api_web_demo, lint_error_after_text_insertion) {
5858
EXPECT_EQ(diagnostics[0].begin_offset, strlen(u8"let x;let "));
5959
EXPECT_EQ(diagnostics[0].end_offset, strlen(u8"let x;let x"));
6060

61-
qljs_web_demo_destroy_parser(p);
61+
qljs_web_demo_destroy_document(p);
6262
}
6363

6464
TEST(test_c_api_vscode, lint_new_error_after_second_text_insertion) {
65-
qljs_vscode_parser* p = qljs_vscode_create_parser();
65+
qljs_vscode_document* p = qljs_vscode_create_document();
6666

6767
const char8* document_text = u8"let x;";
6868
qljs_vscode_replace_text(p, /*start_line=*/0, /*start_character=*/0,
@@ -74,7 +74,7 @@ TEST(test_c_api_vscode, lint_new_error_after_second_text_insertion) {
7474
qljs_vscode_replace_text(p, /*start_line=*/0, /*start_character=*/0,
7575
/*end_line=*/0, /*end_character=*/0, document_text,
7676
strlen(document_text));
77-
// Parser's text: let x;let x;
77+
// Document's text: let x;let x;
7878
diagnostics = qljs_vscode_lint(p);
7979
EXPECT_NE(diagnostics[0].message, nullptr);
8080
EXPECT_EQ(diagnostics[1].message, nullptr);
@@ -87,11 +87,11 @@ TEST(test_c_api_vscode, lint_new_error_after_second_text_insertion) {
8787
EXPECT_EQ(diagnostics[0].end_line, 0);
8888
EXPECT_EQ(diagnostics[0].end_character, strlen(u8"let x;let x"));
8989

90-
qljs_vscode_destroy_parser(p);
90+
qljs_vscode_destroy_document(p);
9191
}
9292

9393
TEST(test_c_api_web_demo, lint_new_error_after_second_text_insertion) {
94-
qljs_web_demo_parser* p = qljs_web_demo_create_parser();
94+
qljs_web_demo_document* p = qljs_web_demo_create_document();
9595

9696
const char8* document_text = u8"let x;";
9797
qljs_web_demo_set_text(p, document_text, strlen(document_text));
@@ -110,11 +110,11 @@ TEST(test_c_api_web_demo, lint_new_error_after_second_text_insertion) {
110110
EXPECT_EQ(diagnostics[0].begin_offset, strlen(u8"let x;let "));
111111
EXPECT_EQ(diagnostics[0].end_offset, strlen(u8"let x;let x"));
112112

113-
qljs_web_demo_destroy_parser(p);
113+
qljs_web_demo_destroy_document(p);
114114
}
115115

116116
TEST(test_c_api_vscode, diagnostic_severity) {
117-
qljs_vscode_parser* p = qljs_vscode_create_parser();
117+
qljs_vscode_document* p = qljs_vscode_create_document();
118118

119119
const char8* document_text = u8"let x;let x;\nundeclaredVariable;";
120120
qljs_vscode_replace_text(p, /*start_line=*/0, /*start_character=*/0,
@@ -131,7 +131,7 @@ TEST(test_c_api_vscode, diagnostic_severity) {
131131
EXPECT_EQ(diagnostics[0].severity, qljs_severity_error);
132132
EXPECT_EQ(diagnostics[1].severity, qljs_severity_warning);
133133

134-
qljs_vscode_destroy_parser(p);
134+
qljs_vscode_destroy_document(p);
135135
}
136136
}
137137
}

wasm/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if (EMSCRIPTEN)
1212
quick-lint-js-vscode
1313
PRIVATE
1414
quick-lint-js-lib
15-
"-sEXPORTED_FUNCTIONS=[\"_qljs_vscode_create_parser\",\"_qljs_vscode_destroy_parser\",\"_qljs_vscode_replace_text\",\"_qljs_vscode_lint\",\"_qljs_web_demo_create_parser\",\"_qljs_web_demo_destroy_parser\",\"_qljs_web_demo_set_text\",\"_qljs_web_demo_lint\",\"_malloc\",\"_free\"]"
15+
"-sEXPORTED_FUNCTIONS=[\"_qljs_vscode_create_document\",\"_qljs_vscode_destroy_document\",\"_qljs_vscode_replace_text\",\"_qljs_vscode_lint\",\"_qljs_web_demo_create_document\",\"_qljs_web_demo_destroy_document\",\"_qljs_web_demo_set_text\",\"_qljs_web_demo_lint\",\"_malloc\",\"_free\"]"
1616
-sASSERTIONS=0
1717
-sSTANDALONE_WASM=1
1818
--no-entry

wasm/cli.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ async function main() {
1616
let factory = await createProcessFactoryAsync();
1717
let qljsProcess = await factory.createProcessAsync();
1818

19-
let parserForVSCode = await qljsProcess.createParserForVSCodeAsync();
19+
let documentForVSCode = await qljsProcess.createDocumentForVSCodeAsync();
2020
try {
21-
parserForVSCode.replaceText(
21+
documentForVSCode.replaceText(
2222
{
2323
start: { line: 0, character: 0 },
2424
end: { line: 0, character: 0 },
2525
},
2626
fileContent
2727
);
28-
let diagnostics = parserForVSCode.lint();
28+
let diagnostics = documentForVSCode.lint();
2929

3030
for (let diag of diagnostics) {
3131
console.log(
@@ -35,13 +35,13 @@ async function main() {
3535
);
3636
}
3737
} finally {
38-
parserForVSCode.dispose();
38+
documentForVSCode.dispose();
3939
}
4040

41-
let parserForWebDemo = await qljsProcess.createParserForWebDemoAsync();
41+
let documentForWebDemo = await qljsProcess.createDocumentForWebDemoAsync();
4242
try {
43-
parserForWebDemo.setText(fileContent);
44-
let diagnostics = parserForWebDemo.lint();
43+
documentForWebDemo.setText(fileContent);
44+
let diagnostics = documentForWebDemo.lint();
4545

4646
for (let diag of diagnostics) {
4747
console.log(
@@ -51,7 +51,7 @@ async function main() {
5151
);
5252
}
5353
} finally {
54-
parserForWebDemo.dispose();
54+
documentForWebDemo.dispose();
5555
}
5656
}
5757

0 commit comments

Comments
 (0)