shareable-twoslash-comments 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +2 -0
  2. package/dist/index.js +58 -23
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  A TypeScript Playground plugin that embeds twoslash (// ^?) type hints as literal comments in your code, making them easy to copy and share.
4
4
 
5
+ https://github.com/user-attachments/assets/2224eec7-3011-4a08-b157-9eb6da700675
6
+
5
7
  ## Running this plugin
6
8
 
7
9
  - [Click this link](https://www.typescriptlang.org/play?install-plugin=shareable-twoslash-comments) to install
package/dist/index.js CHANGED
@@ -60,21 +60,32 @@ define(function () { 'use strict';
60
60
 
61
61
  var twoSlashQueryRegex = /(^[ \t]*)(\/\/\s*\^\?)/gm;
62
62
  var fillTwoSlashQueries = function (sandbox) { return __awaiter(void 0, void 0, void 0, function () {
63
- var multilineEnabled, model, worker, text, editOperations, _i, _a, match, commentPrefix, caretOffset, caretPos, quickInfoPos, quickInfoOffset, quickInfo, quickInfoString, quickInfoComment, prevQuickInfoComment, prevQuickInfoLines, prevQuickInfoEndLine;
64
- return __generator(this, function (_b) {
65
- switch (_b.label) {
63
+ var multilineEnabled, truncationDisabled, model, worker, diagnostics, text, editOperations, _i, _a, match, commentPrefix, caretOffset, caretPos, quickInfoPos, quickInfoOffset, quickInfo, quickInfoString, quickInfoComment, prevQuickInfoComment, prevQuickInfoLines, prevQuickInfoEndLine;
64
+ var _b, _c;
65
+ return __generator(this, function (_d) {
66
+ switch (_d.label) {
66
67
  case 0:
67
68
  multilineEnabled = localStorage.getItem("shareable-twoslash-comments/enable-multiline-comments") === "true";
69
+ truncationDisabled = localStorage.getItem("shareable-twoslash-comments/disable-truncation") === "true";
68
70
  model = sandbox.getModel();
69
71
  return [4 /*yield*/, sandbox.getWorkerProcess()];
70
72
  case 1:
71
- worker = _b.sent();
73
+ worker = _d.sent();
74
+ return [4 /*yield*/, Promise.all([
75
+ worker.getSyntacticDiagnostics("file://" + model.uri.path),
76
+ worker.getSemanticDiagnostics("file://" + model.uri.path),
77
+ ])];
78
+ case 2:
79
+ diagnostics = _d.sent();
80
+ if (diagnostics.flat().length > 0) {
81
+ return [2 /*return*/];
82
+ }
72
83
  text = model.getValue();
73
84
  editOperations = [];
74
85
  _i = 0, _a = Array.from(text.matchAll(twoSlashQueryRegex));
75
- _b.label = 2;
76
- case 2:
77
- if (!(_i < _a.length)) return [3 /*break*/, 5];
86
+ _d.label = 3;
87
+ case 3:
88
+ if (!(_i < _a.length)) return [3 /*break*/, 6];
78
89
  match = _a[_i];
79
90
  commentPrefix = "".concat(match[1], "//").padEnd(match[0].length + 1);
80
91
  caretOffset = match.index + match[0].length - 1;
@@ -82,15 +93,12 @@ define(function () { 'use strict';
82
93
  quickInfoPos = new sandbox.monaco.Position(caretPos.lineNumber - 1, caretPos.column);
83
94
  quickInfoOffset = model.getOffsetAt(quickInfoPos);
84
95
  return [4 /*yield*/, worker.getQuickInfoAtPosition("file://" + model.uri.path, quickInfoOffset)];
85
- case 3:
86
- quickInfo = _b.sent();
87
- if (!(quickInfo === null || quickInfo === void 0 ? void 0 : quickInfo.displayParts)) {
88
- return [3 /*break*/, 4];
89
- }
90
- quickInfoString = quickInfo.displayParts.map(function (d) { return d.text; }).join("");
96
+ case 4:
97
+ quickInfo = _d.sent();
98
+ quickInfoString = (_c = (_b = quickInfo === null || quickInfo === void 0 ? void 0 : quickInfo.displayParts) === null || _b === void 0 ? void 0 : _b.map(function (d) { return d.text; }).join("")) !== null && _c !== void 0 ? _c : "";
91
99
  quickInfoComment = "".concat(match[0], " ").concat(multilineEnabled
92
100
  ? quickInfoString.replace(/\r?\n/g, model.getEOL() + commentPrefix)
93
- : quickInfoString.replace(/\r?\n\s*/g, " "));
101
+ : truncate(quickInfoString.replace(/\r?\n\s*/g, " "), truncationDisabled ? Number.POSITIVE_INFINITY : 100));
94
102
  prevQuickInfoComment = getPreviousQuickInfoComment({
95
103
  model: model,
96
104
  lineNumber: caretPos.lineNumber,
@@ -104,13 +112,13 @@ define(function () { 'use strict';
104
112
  text: quickInfoComment,
105
113
  });
106
114
  }
107
- _b.label = 4;
108
- case 4:
109
- _i++;
110
- return [3 /*break*/, 2];
115
+ _d.label = 5;
111
116
  case 5:
117
+ _i++;
118
+ return [3 /*break*/, 3];
119
+ case 6:
112
120
  if (editOperations.length > 0) {
113
- model.pushEditOperations([], editOperations, function () { return null; });
121
+ model.applyEdits(editOperations);
114
122
  }
115
123
  return [2 /*return*/];
116
124
  }
@@ -118,18 +126,39 @@ define(function () { 'use strict';
118
126
  }); };
119
127
  var getPreviousQuickInfoComment = function (_a) {
120
128
  var model = _a.model, lineNumber = _a.lineNumber, commentPrefix = _a.commentPrefix;
121
- var prevQuickInfoLines = [];
122
- for (var currLineNumber = lineNumber; currLineNumber <= model.getLineCount(); currLineNumber++) {
129
+ var prevQuickInfoLines = [model.getLineContent(lineNumber)];
130
+ for (var currLineNumber = lineNumber + 1; currLineNumber <= model.getLineCount(); currLineNumber++) {
123
131
  var lineContent = model.getLineContent(currLineNumber);
124
- if (!(lineContent.startsWith(commentPrefix) || lineContent.search(twoSlashQueryRegex) !== -1)) {
132
+ if (!lineContent.startsWith(commentPrefix)) {
125
133
  break;
126
134
  }
127
135
  prevQuickInfoLines.push(lineContent);
128
136
  }
129
137
  return prevQuickInfoLines.join(model.getEOL());
130
138
  };
139
+ var debounce = function (callback, delay) {
140
+ if (delay === void 0) { delay = 1000; }
141
+ var timeoutId;
142
+ return function () {
143
+ var args = [];
144
+ for (var _i = 0; _i < arguments.length; _i++) {
145
+ args[_i] = arguments[_i];
146
+ }
147
+ if (timeoutId) {
148
+ clearTimeout(timeoutId);
149
+ }
150
+ timeoutId = setTimeout(function () {
151
+ callback.apply(void 0, args);
152
+ timeoutId = undefined;
153
+ }, delay);
154
+ };
155
+ };
156
+ var truncate = function (str, maxLength) {
157
+ return str.length > maxLength ? str.slice(0, maxLength) + "…" : str;
158
+ };
131
159
  //# sourceMappingURL=utils.js.map
132
160
 
161
+ var debouncedFillTwoSlashQueries = debounce(fillTwoSlashQueries, 600);
133
162
  var makePlugin = function (utils) {
134
163
  var customPlugin = {
135
164
  id: "shareable-twoslash-comments",
@@ -146,6 +175,12 @@ define(function () { 'use strict';
146
175
  display: "Enable multiline comments",
147
176
  onchange: function () { return fillTwoSlashQueries(sandbox); },
148
177
  },
178
+ {
179
+ blurb: "Prevent truncation of single line comments. Otherwise, they will be truncated to 100 characters.",
180
+ flag: "shareable-twoslash-comments/disable-truncation",
181
+ display: "Disable truncation",
182
+ onchange: function () { return fillTwoSlashQueries(sandbox); },
183
+ },
149
184
  ], {
150
185
  style: "separated",
151
186
  });
@@ -156,7 +191,7 @@ define(function () { 'use strict';
156
191
  // and then will always run at the end.
157
192
  modelChangedDebounce: function (sandbox, model) { return __awaiter(void 0, void 0, void 0, function () {
158
193
  return __generator(this, function (_a) {
159
- fillTwoSlashQueries(sandbox);
194
+ debouncedFillTwoSlashQueries(sandbox);
160
195
  return [2 /*return*/];
161
196
  });
162
197
  }); },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shareable-twoslash-comments",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "main": "dist/index.js",
5
5
  "description": "A TypeScript Playground plugin that embeds twoslash (// ^?) type hints as literal comments in your code, making them easy to copy and share.",
6
6
  "license": "MIT",