Skip to content

Commit 1689da5

Browse files
committed
added toasts for wasm-errors
formatting eslint
1 parent 89d5554 commit 1689da5

File tree

8 files changed

+130
-51
lines changed

8 files changed

+130
-51
lines changed

.eslintrc.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ module.exports = {
2222
'plugin:@typescript-eslint/eslint-recommended',
2323
'plugin:@typescript-eslint/recommended-requiring-type-checking',
2424
],
25-
rules: {},
25+
rules: {
26+
'@typescript-eslint/no-unused-vars': [
27+
'warn', // or "error"
28+
{
29+
argsIgnorePattern: '^_',
30+
varsIgnorePattern: '^_',
31+
caughtErrorsIgnorePattern: '^_',
32+
},
33+
],
34+
},
2635
},
2736
],
2837
}

packages/language-server/src/MessageHandler.ts

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ import { createDiagnosticsForIgnore } from './diagnosticsHandler'
7070

7171
export function handleDiagnosticsRequest(
7272
document: TextDocument,
73-
onError?: (errorMessage: string) => void,
73+
showErrorToast?: (errorMessage: string) => void,
7474
): Diagnostic[] {
7575
const text = document.getText(fullDocumentRange(document))
7676
const res = lint(text, (errorMessage: string) => {
77-
if (onError) {
78-
onError(errorMessage)
77+
if (showErrorToast) {
78+
showErrorToast(errorMessage)
7979
}
8080
})
8181

@@ -87,8 +87,8 @@ export function handleDiagnosticsRequest(
8787
diagnostic.text === 'Model declarations have to be indicated with the `model` keyword.',
8888
)
8989
) {
90-
if (onError) {
91-
onError(
90+
if (showErrorToast) {
91+
showErrorToast(
9292
"You might currently be viewing a Prisma 1 datamodel which is based on the GraphQL syntax. The current Prisma Language Server doesn't support this syntax. If you are handling a Prisma 1 datamodel, please change the file extension to `.graphql` so the new Prisma Language Server does not get triggered anymore.",
9393
)
9494
}
@@ -237,10 +237,18 @@ export function handleHoverRequest(document: TextDocument, params: HoverParams):
237237
return
238238
}
239239

240-
function prismaFmtCompletions(params: CompletionParams, document: TextDocument): CompletionList | undefined {
240+
function prismaFmtCompletions(
241+
params: CompletionParams,
242+
document: TextDocument,
243+
showErrorToast?: (errorMessage: string) => void,
244+
): CompletionList | undefined {
241245
const text = document.getText(fullDocumentRange(document))
242246

243-
const completionList = textDocumentCompletion(text, params)
247+
const completionList = textDocumentCompletion(text, params, (errorMessage: string) => {
248+
if (showErrorToast) {
249+
showErrorToast(errorMessage)
250+
}
251+
})
244252

245253
if (completionList.items.length === 0) {
246254
return undefined
@@ -249,7 +257,11 @@ function prismaFmtCompletions(params: CompletionParams, document: TextDocument):
249257
}
250258
}
251259

252-
function localCompletions(params: CompletionParams, document: TextDocument): CompletionList | undefined {
260+
function localCompletions(
261+
params: CompletionParams,
262+
document: TextDocument,
263+
showErrorToast?: (errorMessage: string) => void,
264+
): CompletionList | undefined {
253265
const context = params.context
254266
const position = params.position
255267

@@ -291,6 +303,7 @@ function localCompletions(params: CompletionParams, document: TextDocument): Com
291303
lines,
292304
wordsBeforePosition,
293305
document,
306+
showErrorToast,
294307
)
295308
case '"':
296309
return getSuggestionForSupportedFields(
@@ -303,13 +316,10 @@ function localCompletions(params: CompletionParams, document: TextDocument): Com
303316
case '.':
304317
// check if inside attribute
305318
// Useful to complete composite types
306-
if (
307-
['model', 'view'].includes(foundBlock.type) &&
308-
isInsideAttribute(currentLineUntrimmed, position, '()')
309-
) {
319+
if (['model', 'view'].includes(foundBlock.type) && isInsideAttribute(currentLineUntrimmed, position, '()')) {
310320
return getSuggestionsForInsideRoundBrackets(currentLineUntrimmed, lines, document, position, foundBlock)
311321
} else {
312-
return getSuggestionForNativeTypes(foundBlock, lines, wordsBeforePosition, document)
322+
return getSuggestionForNativeTypes(foundBlock, lines, wordsBeforePosition, document, showErrorToast)
313323
}
314324
}
315325
}
@@ -326,7 +336,14 @@ function localCompletions(params: CompletionParams, document: TextDocument): Com
326336
if (!positionIsAfterFieldAndType(position, document, wordsBeforePosition)) {
327337
return getSuggestionsForFieldTypes(foundBlock, lines, position, currentLineUntrimmed)
328338
}
329-
return getSuggestionForFieldAttribute(foundBlock, lines[position.line], lines, wordsBeforePosition, document)
339+
return getSuggestionForFieldAttribute(
340+
foundBlock,
341+
lines[position.line],
342+
lines,
343+
wordsBeforePosition,
344+
document,
345+
showErrorToast,
346+
)
330347
case 'datasource':
331348
case 'generator':
332349
if (wordsBeforePosition.length === 1 && symbolBeforePositionIsWhiteSpace) {
@@ -356,8 +373,12 @@ function localCompletions(params: CompletionParams, document: TextDocument): Com
356373
*
357374
* This handler provides the initial list of the completion items.
358375
*/
359-
export function handleCompletionRequest(params: CompletionParams, document: TextDocument): CompletionList | undefined {
360-
return prismaFmtCompletions(params, document) || localCompletions(params, document)
376+
export function handleCompletionRequest(
377+
params: CompletionParams,
378+
document: TextDocument,
379+
showErrorToast?: (errorMessage: string) => void,
380+
): CompletionList | undefined {
381+
return prismaFmtCompletions(params, document, showErrorToast) || localCompletions(params, document, showErrorToast)
361382
}
362383

363384
export function handleRenameRequest(params: RenameParams, document: TextDocument): WorkspaceEdit | undefined {
@@ -453,12 +474,16 @@ export function handleCompletionResolveRequest(item: CompletionItem): Completion
453474
return item
454475
}
455476

456-
export function handleCodeActions(params: CodeActionParams, document: TextDocument): CodeAction[] {
477+
export function handleCodeActions(
478+
params: CodeActionParams,
479+
document: TextDocument,
480+
showErrorToast?: (errorMessage: string) => void,
481+
): CodeAction[] {
457482
if (!params.context.diagnostics.length) {
458483
return []
459484
}
460485

461-
return quickFix(document, params)
486+
return quickFix(document, params, showErrorToast)
462487
}
463488

464489
export function handleDocumentSymbol(params: DocumentSymbolParams, document: TextDocument): DocumentSymbol[] {

packages/language-server/src/codeActionProvider.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from 'vscode-languageserver'
1010
import levenshtein from 'js-levenshtein'
1111
import { convertDocumentTextToTrimmedLineArray, getAllRelationNames } from './util'
12-
import { prismaFmt } from './wasm'
12+
import codeActions from './prisma-fmt/codeActions'
1313

1414
function getInsertRange(document: TextDocument): Range {
1515
// to insert text into a document create a range where start === end.
@@ -81,19 +81,22 @@ function addTypeModifiers(hasTypeModifierArray: boolean, hasTypeModifierOptional
8181
return suggestion
8282
}
8383

84-
export function quickFix(textDocument: TextDocument, params: CodeActionParams): CodeAction[] {
84+
export function quickFix(
85+
textDocument: TextDocument,
86+
params: CodeActionParams,
87+
showErrorToast?: (errorMessage: string) => void,
88+
): CodeAction[] {
8589
const diagnostics: Diagnostic[] = params.context.diagnostics
8690

8791
if (!diagnostics || diagnostics.length === 0) {
8892
return []
8993
}
9094

91-
// get code actions from prisma-fmt
92-
const actionDataFromPrismaFmt: string = prismaFmt.code_actions(textDocument.getText(), JSON.stringify(params))
93-
// For debugging
94-
// console.log({ actionDataFromPrismaFmt })
95-
96-
const codeActions: CodeAction[] = JSON.parse(actionDataFromPrismaFmt) as CodeAction[]
95+
const codeActionList = codeActions(textDocument.getText(), JSON.stringify(params), (errorMessage: string) => {
96+
if (showErrorToast) {
97+
showErrorToast(errorMessage)
98+
}
99+
})
97100

98101
// Add code actions from typescript side
99102
for (const diag of diagnostics) {
@@ -109,7 +112,7 @@ export function quickFix(textDocument: TextDocument, params: CodeActionParams):
109112
const lines: string[] = convertDocumentTextToTrimmedLineArray(textDocument)
110113
const spellingSuggestion = getSpellingSuggestions(diagText, getAllRelationNames(lines))
111114
if (spellingSuggestion) {
112-
codeActions.push({
115+
codeActionList.push({
113116
title: `Change spelling to '${spellingSuggestion}'`,
114117
kind: CodeActionKind.QuickFix,
115118
diagnostics: [diag],
@@ -125,7 +128,7 @@ export function quickFix(textDocument: TextDocument, params: CodeActionParams):
125128
},
126129
})
127130
}
128-
codeActions.push({
131+
codeActionList.push({
129132
title: `Create new model '${diagText}'`,
130133
kind: CodeActionKind.QuickFix,
131134
diagnostics: [diag],
@@ -140,7 +143,7 @@ export function quickFix(textDocument: TextDocument, params: CodeActionParams):
140143
},
141144
},
142145
})
143-
codeActions.push({
146+
codeActionList.push({
144147
title: `Create new enum '${diagText}'`,
145148
kind: CodeActionKind.QuickFix,
146149
diagnostics: [diag],
@@ -159,7 +162,7 @@ export function quickFix(textDocument: TextDocument, params: CodeActionParams):
159162
diag.severity === DiagnosticSeverity.Warning &&
160163
diag.message.includes("property has been renamed to 'previewFeatures'")
161164
) {
162-
codeActions.push({
165+
codeActionList.push({
163166
title: "Rename property to 'previewFeatures'",
164167
kind: CodeActionKind.QuickFix,
165168
diagnostics: [diag],
@@ -182,7 +185,7 @@ export function quickFix(textDocument: TextDocument, params: CodeActionParams):
182185
if (diagText.length !== 0) {
183186
const spellingSuggestion = getSpellingSuggestions(diagText[0], ['model', 'enum', 'datasource', 'generator'])
184187
if (spellingSuggestion) {
185-
codeActions.push({
188+
codeActionList.push({
186189
title: `Change spelling to '${spellingSuggestion}'`,
187190
kind: CodeActionKind.QuickFix,
188191
diagnostics: [diag],
@@ -209,5 +212,5 @@ export function quickFix(textDocument: TextDocument, params: CodeActionParams):
209212
}
210213
}
211214

212-
return codeActions
215+
return codeActionList
213216
}

packages/language-server/src/completion/completionUtils.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export function givenBlockAttributeParams({
121121
blockAttribute,
122122
wordBeforePosition,
123123
datasourceProvider,
124+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
124125
previewFeatures,
125126
}: {
126127
blockAttribute: '@@unique' | '@@id' | '@@index' | '@@fulltext'
@@ -327,6 +328,7 @@ export const fieldAttributes: CompletionItem[] = convertAttributesToCompletionIt
327328
export const sortLengthProperties: CompletionItem[] =
328329
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
329330
convertToCompletionItems(
331+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
330332
completions.fieldAttributes
331333
.find((item) => item.label === '@unique')!
332334
.params.filter((item) => item.label === 'length' || item.label === 'sort'),
@@ -513,8 +515,16 @@ export function handlePreviewFeatures(
513515
}
514516
}
515517

516-
export function getNativeTypes(document: TextDocument, prismaType: string): CompletionItem[] {
517-
let nativeTypes: NativeTypeConstructors[] = nativeTypeConstructors(document.getText())
518+
export function getNativeTypes(
519+
document: TextDocument,
520+
prismaType: string,
521+
showErrorToast?: (errorMessage: string) => void,
522+
): CompletionItem[] {
523+
let nativeTypes: NativeTypeConstructors[] = nativeTypeConstructors(document.getText(), (errorMessage: string) => {
524+
if (showErrorToast) {
525+
showErrorToast(errorMessage)
526+
}
527+
})
518528

519529
if (nativeTypes.length === 0) {
520530
console.log('Did not receive any native type suggestions from prisma-fmt call.')

packages/language-server/src/completion/completions.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ export function getSuggestionForNativeTypes(
117117
lines: string[],
118118
wordsBeforePosition: string[],
119119
document: TextDocument,
120+
showErrorToast?: (errorMessage: string) => void,
120121
): CompletionList | undefined {
121-
const activeFeatureFlag = declaredNativeTypes(document)
122+
const activeFeatureFlag = declaredNativeTypes(document, showErrorToast)
123+
122124
if (
123125
// TODO type? native "@db." types?
124126
foundBlock.type !== 'model' ||
@@ -135,7 +137,7 @@ export function getSuggestionForNativeTypes(
135137

136138
// line
137139
const prismaType = wordsBeforePosition[1].replace('?', '').replace('[]', '')
138-
const suggestions = getNativeTypes(document, prismaType)
140+
const suggestions = getNativeTypes(document, prismaType, showErrorToast)
139141

140142
return {
141143
items: suggestions,
@@ -161,6 +163,7 @@ export function getSuggestionForFieldAttribute(
161163
lines: string[],
162164
wordsBeforePosition: string[],
163165
document: TextDocument,
166+
showErrorToast?: (errorMessage: string) => void,
164167
): CompletionList | undefined {
165168
const fieldType = getFieldType(currentLine)
166169
// If we don't find a field type (e.g. String, Int...), return no suggestion
@@ -174,7 +177,7 @@ export function getSuggestionForFieldAttribute(
174177
if (wordsBeforePosition.length >= 2) {
175178
const datasourceName = getFirstDatasourceName(lines)
176179
const prismaType = wordsBeforePosition[1]
177-
const nativeTypeSuggestions = getNativeTypes(document, prismaType)
180+
const nativeTypeSuggestions = getNativeTypes(document, prismaType, showErrorToast)
178181

179182
if (datasourceName) {
180183
if (!currentLine.includes(`@${datasourceName}`)) {
@@ -822,6 +825,7 @@ function getSuggestionsForAttribute(
822825
let name = value
823826
// Example for `@@index([email,address.|])` when there is no space between fields
824827
if (name?.includes(',')) {
828+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
825829
name = name.split(',').pop()!
826830
}
827831
// Remove . to only get the name
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { CodeAction } from 'vscode-languageserver'
2+
import { prismaFmt } from '../wasm'
3+
import { handleWasmError } from './util'
4+
5+
export default function codeActions(
6+
schema: string,
7+
params: string,
8+
onError?: (errorMessage: string) => void,
9+
): CodeAction[] {
10+
try {
11+
const result = prismaFmt.code_actions(schema, params)
12+
13+
return JSON.parse(result) as CodeAction[]
14+
} catch (e) {
15+
const err = e as Error
16+
17+
handleWasmError(err, 'code_actions', onError)
18+
19+
return []
20+
}
21+
}

0 commit comments

Comments
 (0)