Skip to content

Commit c912508

Browse files
committed
Rename 'vscode' module to 'c-api'
I want to generalize the qljs_vscode_* and <quick-lint-js/vscode.h> APIs to be usable in more contexts. (For example, it'd be nice if the web demo used the same code.) Rename vscode.h to c-api.h, and rename qljs_vscode_* to qljs_*. qljs_vscode_diagnostic keeps its name because the location information it conveys is specific to what VS Code expects. Other consumers of the API might want different formats. This commit should not change behavior.
1 parent eb479bb commit c912508

File tree

10 files changed

+84
-85
lines changed

10 files changed

+84
-85
lines changed

plugin/vscode/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if (EMSCRIPTEN)
1111
quick-lint-js-vscode
1212
PRIVATE
1313
quick-lint-js-lib
14-
"-sEXPORTED_FUNCTIONS=[\"_qljs_vscode_create_parser\",\"_qljs_vscode_destroy_parser\",\"_qljs_vscode_replace_text\",\"_qljs_vscode_lint\",\"_malloc\",\"_free\"]"
14+
"-sEXPORTED_FUNCTIONS=[\"_qljs_create_parser\",\"_qljs_destroy_parser\",\"_qljs_replace_text\",\"_qljs_lint_vscode\",\"_malloc\",\"_free\"]"
1515
-sASSERTIONS=0
1616
-sSTANDALONE_WASM=1
1717
--no-entry

plugin/vscode/quick-lint-js.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,20 @@ class Process {
115115
this._malloc = wrap(wasmInstance.exports.malloc, "malloc");
116116
this._free = wrap(wasmInstance.exports.free, "free");
117117
this._createParser = wrap(
118-
wasmInstance.exports.qljs_vscode_create_parser,
119-
"qljs_vscode_create_parser"
118+
wasmInstance.exports.qljs_create_parser,
119+
"qljs_create_parser"
120120
);
121121
this._destroyParser = wrap(
122-
wasmInstance.exports.qljs_vscode_destroy_parser,
123-
"qljs_vscode_destroy_parser"
122+
wasmInstance.exports.qljs_destroy_parser,
123+
"qljs_destroy_parser"
124124
);
125125
this._replaceText = wrap(
126-
wasmInstance.exports.qljs_vscode_replace_text,
127-
"qljs_vscode_replace_text"
126+
wasmInstance.exports.qljs_replace_text,
127+
"qljs_replace_text"
128128
);
129-
this._lint = wrap(
130-
wasmInstance.exports.qljs_vscode_lint,
131-
"qljs_vscode_lint"
129+
this._lintVSCode = wrap(
130+
wasmInstance.exports.qljs_lint_vscode,
131+
"qljs_lint_vscode"
132132
);
133133
}
134134

@@ -161,7 +161,7 @@ class Parser {
161161
}
162162

163163
lint() {
164-
let diagnosticsPointer = this._process._lint(this._parser);
164+
let diagnosticsPointer = this._process._lintVSCode(this._parser);
165165

166166
let rawDiagnosticsU32 = new Uint32Array(
167167
this._process._heap,

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_create_parser failures
270270
// cause this test to fail.
271-
if (functionName !== "qljs_vscode_create_parser") {
271+
if (functionName !== "qljs_create_parser") {
272272
let shouldCrash = rng.nextCoinFlip();
273273
coinFlips.push(shouldCrash);
274274
if (shouldCrash) {

src/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ quick_lint_js_add_library(
2929
assert.cpp
3030
buffering-error-reporter.cpp
3131
byte-buffer.cpp
32+
c-api.cpp
3233
char8.cpp
3334
cli-location.cpp
3435
crash.cpp
@@ -61,6 +62,7 @@ quick_lint_js_add_library(
6162
quick-lint-js/buffering-error-reporter.h
6263
quick-lint-js/buffering-visitor.h
6364
quick-lint-js/byte-buffer.h
65+
quick-lint-js/c-api.h
6466
quick-lint-js/char8.h
6567
quick-lint-js/cli-location.h
6668
quick-lint-js/constant-divider.h
@@ -113,7 +115,6 @@ quick_lint_js_add_library(
113115
quick-lint-js/vim-location.h
114116
quick-lint-js/vim-qflist-json-error-reporter.h
115117
quick-lint-js/vscode-error-reporter.h
116-
quick-lint-js/vscode.h
117118
quick-lint-js/warning.h
118119
quick-lint-js/wasm-demo-error-reporter.h
119120
quick-lint-js/wasm-demo-location.h
@@ -126,7 +127,6 @@ quick_lint_js_add_library(
126127
vim-location.cpp
127128
vim-qflist-json-error-reporter.cpp
128129
vscode-error-reporter.cpp
129-
vscode.cpp
130130
wasm-demo-error-reporter.cpp
131131
wasm-demo-location.cpp
132132
)

src/vscode.cpp renamed to src/c-api.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// See end of file for extended copyright information.
33

44
#include <cstddef>
5+
#include <quick-lint-js/c-api.h>
56
#include <quick-lint-js/char8.h>
67
#include <quick-lint-js/error.h>
78
#include <quick-lint-js/lint.h>
@@ -10,11 +11,10 @@
1011
#include <quick-lint-js/padded-string.h>
1112
#include <quick-lint-js/parse.h>
1213
#include <quick-lint-js/vscode-error-reporter.h>
13-
#include <quick-lint-js/vscode.h>
1414

15-
struct qljs_vscode_parser {
15+
struct qljs_parser {
1616
public:
17-
const qljs_vscode_diagnostic* lint() {
17+
const qljs_vscode_diagnostic* lint_vscode() {
1818
return this->error_reporter_.get_diagnostics();
1919
}
2020

@@ -45,27 +45,26 @@ struct qljs_vscode_parser {
4545
quick_lint_js::vscode_error_reporter error_reporter_;
4646
};
4747

48-
qljs_vscode_parser* qljs_vscode_create_parser(void) {
49-
qljs_vscode_parser* p = new qljs_vscode_parser();
48+
qljs_parser* qljs_create_parser(void) {
49+
qljs_parser* p = new qljs_parser();
5050
return p;
5151
}
5252

53-
void qljs_vscode_destroy_parser(qljs_vscode_parser* p) { delete p; }
53+
void qljs_destroy_parser(qljs_parser* p) { delete p; }
5454

55-
void qljs_vscode_replace_text(qljs_vscode_parser* p, int start_line,
56-
int start_character, int end_line,
57-
int end_character,
58-
const void* replacement_text_utf_8,
59-
size_t replacement_text_byte_count) {
55+
void qljs_replace_text(qljs_parser* p, int start_line, int start_character,
56+
int end_line, int end_character,
57+
const void* replacement_text_utf_8,
58+
size_t replacement_text_byte_count) {
6059
p->replace_text(
6160
start_line, start_character, end_line, end_character,
6261
quick_lint_js::string8_view(
6362
reinterpret_cast<const quick_lint_js::char8*>(replacement_text_utf_8),
6463
replacement_text_byte_count));
6564
}
6665

67-
const qljs_vscode_diagnostic* qljs_vscode_lint(qljs_vscode_parser* p) {
68-
return p->lint();
66+
const qljs_vscode_diagnostic* qljs_lint_vscode(qljs_parser* p) {
67+
return p->lint_vscode();
6968
}
7069

7170
// quick-lint-js finds bugs in JavaScript programs.

src/quick-lint-js/vscode.h renamed to src/quick-lint-js/c-api.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
// Copyright (C) 2020 Matthew Glazar
22
// See end of file for extended copyright information.
33

4-
#ifndef QUICK_LINT_JS_VSCODE_H
5-
#define QUICK_LINT_JS_VSCODE_H
4+
#ifndef QUICK_LINT_JS_C_API_H
5+
#define QUICK_LINT_JS_C_API_H
66

77
#include <stddef.h>
88

99
#if defined(__cplusplus)
1010
extern "C" {
1111
#endif
1212

13-
typedef struct qljs_vscode_parser qljs_vscode_parser;
13+
typedef struct qljs_parser qljs_parser;
1414

15-
typedef enum qljs_vscode_severity {
16-
qljs_vscode_severity_error = 1,
17-
qljs_vscode_severity_warning = 2,
18-
} qljs_vscode_severity;
15+
typedef enum qljs_severity {
16+
qljs_severity_error = 1,
17+
qljs_severity_warning = 2,
18+
} qljs_severity;
1919

20-
qljs_vscode_parser* qljs_vscode_create_parser(void);
21-
void qljs_vscode_destroy_parser(qljs_vscode_parser*);
20+
qljs_parser* qljs_create_parser(void);
2221

23-
void qljs_vscode_replace_text(qljs_vscode_parser*, int start_line,
24-
int start_character, int end_line,
25-
int end_character,
26-
const void* replacement_text_utf_8,
27-
size_t replacement_text_byte_count);
22+
void qljs_destroy_parser(qljs_parser*);
23+
24+
void qljs_replace_text(qljs_parser*, int start_line, int start_character,
25+
int end_line, int end_character,
26+
const void* replacement_text_utf_8,
27+
size_t replacement_text_byte_count);
2828

2929
struct qljs_vscode_diagnostic {
3030
const char* message;
3131
const char* code;
32-
qljs_vscode_severity severity;
32+
qljs_severity severity;
3333
int start_line;
3434
int start_character;
3535
int end_line;
3636
int end_character;
3737
};
3838

39-
const qljs_vscode_diagnostic* qljs_vscode_lint(qljs_vscode_parser*);
39+
const qljs_vscode_diagnostic* qljs_lint_vscode(qljs_parser*);
4040

4141
#if defined(__cplusplus)
4242
}

src/vscode-error-reporter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <iostream>
55
#include <memory>
6+
#include <quick-lint-js/c-api.h>
67
#include <quick-lint-js/error.h>
78
#include <quick-lint-js/location.h>
89
#include <quick-lint-js/lsp-location.h>
@@ -12,7 +13,6 @@
1213
#include <quick-lint-js/token.h>
1314
#include <quick-lint-js/unreachable.h>
1415
#include <quick-lint-js/vscode-error-reporter.h>
15-
#include <quick-lint-js/vscode.h>
1616
#include <string>
1717

1818
namespace quick_lint_js {
@@ -79,16 +79,16 @@ void vscode_error_formatter::write_message_part(severity sev,
7979

8080
void vscode_error_formatter::write_after_message(
8181
severity sev, const source_code_span &origin) {
82-
qljs_vscode_severity diag_severity = qljs_vscode_severity_error;
82+
qljs_severity diag_severity = qljs_severity_error;
8383
switch (sev) {
8484
case severity::note:
8585
// Don't write notes. Only write the main message.
8686
return;
8787
case severity::error:
88-
diag_severity = qljs_vscode_severity_error;
88+
diag_severity = qljs_severity_error;
8989
break;
9090
case severity::warning:
91-
diag_severity = qljs_vscode_severity_warning;
91+
diag_severity = qljs_severity_warning;
9292
break;
9393
}
9494
qljs_vscode_diagnostic &diag = this->reporter_->diagnostics_.emplace_back();

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ quick_lint_js_add_executable(
2828
test-buffering-error-reporter.cpp
2929
test-buffering-visitor.cpp
3030
test-byte-buffer.cpp
31+
test-c-api.cpp
3132
test-cli-location.cpp
3233
test-constant-divider.cpp
3334
test-crash.cpp
@@ -75,7 +76,6 @@ quick_lint_js_add_executable(
7576
test-vim-location.cpp
7677
test-vim-qflist-json-error-reporter.cpp
7778
test-vscode-error-reporter.cpp
78-
test-vscode.cpp
7979
test-wasm-demo-error-reporter.cpp
8080
test-wasm-demo-location.cpp
8181
test-write-integer.cpp

test/test-vscode.cpp renamed to test/test-c-api.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22
// See end of file for extended copyright information.
33

44
#include <gtest/gtest.h>
5+
#include <quick-lint-js/c-api.h>
56
#include <quick-lint-js/char8.h>
6-
#include <quick-lint-js/vscode.h>
77

88
namespace quick_lint_js {
99
namespace {
10-
TEST(test_vscode, empty_document_has_no_diagnostics) {
11-
qljs_vscode_parser* p = qljs_vscode_create_parser();
12-
const qljs_vscode_diagnostic* diagnostics = qljs_vscode_lint(p);
10+
TEST(test_c_api, empty_document_has_no_diagnostics) {
11+
qljs_parser* p = qljs_create_parser();
12+
const qljs_vscode_diagnostic* diagnostics = qljs_lint_vscode(p);
1313
EXPECT_EQ(diagnostics[0].message, nullptr);
14-
qljs_vscode_destroy_parser(p);
14+
qljs_destroy_parser(p);
1515
}
1616

17-
TEST(test_vscode, lint_error_after_text_insertion) {
18-
qljs_vscode_parser* p = qljs_vscode_create_parser();
17+
TEST(test_c_api, lint_error_after_text_insertion) {
18+
qljs_parser* p = qljs_create_parser();
1919

2020
const char8* document_text = u8"let x;let x;";
21-
qljs_vscode_replace_text(p, /*start_line=*/0, /*start_character=*/0,
22-
/*end_line=*/1, /*end_character=*/0, document_text,
23-
strlen(document_text));
24-
const qljs_vscode_diagnostic* diagnostics = qljs_vscode_lint(p);
21+
qljs_replace_text(p, /*start_line=*/0, /*start_character=*/0,
22+
/*end_line=*/1, /*end_character=*/0, document_text,
23+
strlen(document_text));
24+
const qljs_vscode_diagnostic* diagnostics = qljs_lint_vscode(p);
2525
EXPECT_NE(diagnostics[0].message, nullptr);
2626
EXPECT_EQ(diagnostics[1].message, nullptr);
2727
EXPECT_EQ(diagnostics[1].code, nullptr);
@@ -33,24 +33,24 @@ TEST(test_vscode, lint_error_after_text_insertion) {
3333
EXPECT_EQ(diagnostics[0].end_line, 0);
3434
EXPECT_EQ(diagnostics[0].end_character, strlen(u8"let x;let x"));
3535

36-
qljs_vscode_destroy_parser(p);
36+
qljs_destroy_parser(p);
3737
}
3838

39-
TEST(test_vscode, lint_new_error_after_second_text_insertion) {
40-
qljs_vscode_parser* p = qljs_vscode_create_parser();
39+
TEST(test_c_api, lint_new_error_after_second_text_insertion) {
40+
qljs_parser* p = qljs_create_parser();
4141

4242
const char8* document_text = u8"let x;";
43-
qljs_vscode_replace_text(p, /*start_line=*/0, /*start_character=*/0,
44-
/*end_line=*/1, /*end_character=*/0, document_text,
45-
strlen(document_text));
46-
const qljs_vscode_diagnostic* diagnostics = qljs_vscode_lint(p);
43+
qljs_replace_text(p, /*start_line=*/0, /*start_character=*/0,
44+
/*end_line=*/1, /*end_character=*/0, document_text,
45+
strlen(document_text));
46+
const qljs_vscode_diagnostic* diagnostics = qljs_lint_vscode(p);
4747
EXPECT_EQ(diagnostics[0].message, nullptr);
4848

49-
qljs_vscode_replace_text(p, /*start_line=*/0, /*start_character=*/0,
50-
/*end_line=*/0, /*end_character=*/0, document_text,
51-
strlen(document_text));
49+
qljs_replace_text(p, /*start_line=*/0, /*start_character=*/0,
50+
/*end_line=*/0, /*end_character=*/0, document_text,
51+
strlen(document_text));
5252
// Parser's text: let x;let x;
53-
diagnostics = qljs_vscode_lint(p);
53+
diagnostics = qljs_lint_vscode(p);
5454
EXPECT_NE(diagnostics[0].message, nullptr);
5555
EXPECT_EQ(diagnostics[1].message, nullptr);
5656
EXPECT_EQ(diagnostics[1].code, nullptr);
@@ -62,28 +62,28 @@ TEST(test_vscode, lint_new_error_after_second_text_insertion) {
6262
EXPECT_EQ(diagnostics[0].end_line, 0);
6363
EXPECT_EQ(diagnostics[0].end_character, strlen(u8"let x;let x"));
6464

65-
qljs_vscode_destroy_parser(p);
65+
qljs_destroy_parser(p);
6666
}
6767

68-
TEST(test_vscode, diagnostic_severity) {
69-
qljs_vscode_parser* p = qljs_vscode_create_parser();
68+
TEST(test_c_api, diagnostic_severity) {
69+
qljs_parser* p = qljs_create_parser();
7070

7171
const char8* document_text = u8"let x;let x;\nundeclaredVariable;";
72-
qljs_vscode_replace_text(p, /*start_line=*/0, /*start_character=*/0,
73-
/*end_line=*/1, /*end_character=*/0, document_text,
74-
strlen(document_text));
75-
const qljs_vscode_diagnostic* diagnostics = qljs_vscode_lint(p);
72+
qljs_replace_text(p, /*start_line=*/0, /*start_character=*/0,
73+
/*end_line=*/1, /*end_character=*/0, document_text,
74+
strlen(document_text));
75+
const qljs_vscode_diagnostic* diagnostics = qljs_lint_vscode(p);
7676
EXPECT_NE(diagnostics[0].message, nullptr);
7777
EXPECT_NE(diagnostics[0].code, nullptr);
7878
EXPECT_NE(diagnostics[1].message, nullptr);
7979
EXPECT_NE(diagnostics[1].code, nullptr);
8080
EXPECT_EQ(diagnostics[2].message, nullptr);
8181
EXPECT_EQ(diagnostics[2].code, nullptr);
8282

83-
EXPECT_EQ(diagnostics[0].severity, qljs_vscode_severity_error);
84-
EXPECT_EQ(diagnostics[1].severity, qljs_vscode_severity_warning);
83+
EXPECT_EQ(diagnostics[0].severity, qljs_severity_error);
84+
EXPECT_EQ(diagnostics[1].severity, qljs_severity_warning);
8585

86-
qljs_vscode_destroy_parser(p);
86+
qljs_destroy_parser(p);
8787
}
8888
}
8989
}

0 commit comments

Comments
 (0)