mirror of
https://github.com/danbulant/monaco-yaml
synced 2026-07-06 11:30:46 +00:00
Convert promise chains to async/await
This commit is contained in:
parent
57e820ed0c
commit
ba395c9d23
2 changed files with 81 additions and 103 deletions
|
|
@ -55,26 +55,19 @@ export function createDiagnosticsAdapter(
|
||||||
let disposables: IDisposable[] = [];
|
let disposables: IDisposable[] = [];
|
||||||
const listeners: Record<string, IDisposable> = Object.create(null);
|
const listeners: Record<string, IDisposable> = Object.create(null);
|
||||||
|
|
||||||
const resetSchema = (resource: Uri): void => {
|
const resetSchema = async (resource: Uri): Promise<void> => {
|
||||||
getWorker().then((worker) => {
|
const worker = await getWorker();
|
||||||
worker.resetSchema(String(resource));
|
worker.resetSchema(String(resource));
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const doValidate = (resource: Uri, languageId: string): void => {
|
const doValidate = async (resource: Uri, languageId: string): Promise<void> => {
|
||||||
getWorker(resource)
|
const worker = await getWorker(resource);
|
||||||
.then((worker) =>
|
const diagnostics = await worker.doValidation(String(resource));
|
||||||
worker.doValidation(String(resource)).then((diagnostics) => {
|
const markers = diagnostics.map((d) => toDiagnostics(resource, d));
|
||||||
const markers = diagnostics.map((d) => toDiagnostics(resource, d));
|
const model = editor.getModel(resource);
|
||||||
const model = editor.getModel(resource);
|
if (model.getModeId() === languageId) {
|
||||||
if (model.getModeId() === languageId) {
|
editor.setModelMarkers(model, languageId, markers);
|
||||||
editor.setModelMarkers(model, languageId, markers);
|
}
|
||||||
}
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.then(undefined, (err) => {
|
|
||||||
console.error(err);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const onModelAdd = (model: editor.IModel): void => {
|
const onModelAdd = (model: editor.IModel): void => {
|
||||||
|
|
@ -223,58 +216,53 @@ export function createCompletionItemProvider(
|
||||||
return {
|
return {
|
||||||
triggerCharacters: [' ', ':'],
|
triggerCharacters: [' ', ':'],
|
||||||
|
|
||||||
provideCompletionItems(
|
async provideCompletionItems(model, position) {
|
||||||
model: editor.IReadOnlyModel,
|
|
||||||
position: Position,
|
|
||||||
): PromiseLike<languages.CompletionList> {
|
|
||||||
const resource = model.uri;
|
const resource = model.uri;
|
||||||
|
|
||||||
return getWorker(resource)
|
const worker = await getWorker(resource);
|
||||||
.then((worker) => worker.doComplete(String(resource), fromPosition(position)))
|
const info = await worker.doComplete(String(resource), fromPosition(position));
|
||||||
.then((info) => {
|
if (!info) {
|
||||||
if (!info) {
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const wordInfo = model.getWordUntilPosition(position);
|
const wordInfo = model.getWordUntilPosition(position);
|
||||||
const wordRange = new Range(
|
const wordRange = new Range(
|
||||||
position.lineNumber,
|
position.lineNumber,
|
||||||
wordInfo.startColumn,
|
wordInfo.startColumn,
|
||||||
position.lineNumber,
|
position.lineNumber,
|
||||||
wordInfo.endColumn,
|
wordInfo.endColumn,
|
||||||
|
);
|
||||||
|
|
||||||
|
const items = info.items.map((entry) => {
|
||||||
|
const item: languages.CompletionItem = {
|
||||||
|
label: entry.label,
|
||||||
|
insertText: entry.insertText || entry.label,
|
||||||
|
sortText: entry.sortText,
|
||||||
|
filterText: entry.filterText,
|
||||||
|
documentation: entry.documentation,
|
||||||
|
detail: entry.detail,
|
||||||
|
kind: toCompletionItemKind(entry.kind),
|
||||||
|
range: wordRange,
|
||||||
|
};
|
||||||
|
if (entry.textEdit) {
|
||||||
|
item.range = toRange(
|
||||||
|
'range' in entry.textEdit ? entry.textEdit.range : entry.textEdit.replace,
|
||||||
);
|
);
|
||||||
|
item.insertText = entry.textEdit.newText;
|
||||||
|
}
|
||||||
|
if (entry.additionalTextEdits) {
|
||||||
|
item.additionalTextEdits = entry.additionalTextEdits.map(toTextEdit);
|
||||||
|
}
|
||||||
|
if (entry.insertTextFormat === ls.InsertTextFormat.Snippet) {
|
||||||
|
item.insertTextRules = languages.CompletionItemInsertTextRule.InsertAsSnippet;
|
||||||
|
}
|
||||||
|
return item;
|
||||||
|
});
|
||||||
|
|
||||||
const items = info.items.map((entry) => {
|
return {
|
||||||
const item: languages.CompletionItem = {
|
incomplete: info.isIncomplete,
|
||||||
label: entry.label,
|
suggestions: items,
|
||||||
insertText: entry.insertText || entry.label,
|
};
|
||||||
sortText: entry.sortText,
|
|
||||||
filterText: entry.filterText,
|
|
||||||
documentation: entry.documentation,
|
|
||||||
detail: entry.detail,
|
|
||||||
kind: toCompletionItemKind(entry.kind),
|
|
||||||
range: wordRange,
|
|
||||||
};
|
|
||||||
if (entry.textEdit) {
|
|
||||||
item.range = toRange(
|
|
||||||
'range' in entry.textEdit ? entry.textEdit.range : entry.textEdit.replace,
|
|
||||||
);
|
|
||||||
item.insertText = entry.textEdit.newText;
|
|
||||||
}
|
|
||||||
if (entry.additionalTextEdits) {
|
|
||||||
item.additionalTextEdits = entry.additionalTextEdits.map(toTextEdit);
|
|
||||||
}
|
|
||||||
if (entry.insertTextFormat === ls.InsertTextFormat.Snippet) {
|
|
||||||
item.insertTextRules = languages.CompletionItemInsertTextRule.InsertAsSnippet;
|
|
||||||
}
|
|
||||||
return item;
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
isIncomplete: info.isIncomplete,
|
|
||||||
suggestions: items,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -319,20 +307,18 @@ function toMarkedStringArray(
|
||||||
|
|
||||||
export function createHoverProvider(getWorker: WorkerAccessor): languages.HoverProvider {
|
export function createHoverProvider(getWorker: WorkerAccessor): languages.HoverProvider {
|
||||||
return {
|
return {
|
||||||
provideHover(model, position) {
|
async provideHover(model, position) {
|
||||||
const resource = model.uri;
|
const resource = model.uri;
|
||||||
|
|
||||||
return getWorker(resource)
|
const worker = await getWorker(resource);
|
||||||
.then((worker) => worker.doHover(String(resource), fromPosition(position)))
|
const info = await worker.doHover(String(resource), fromPosition(position));
|
||||||
.then((info) => {
|
if (!info) {
|
||||||
if (!info) {
|
return;
|
||||||
return;
|
}
|
||||||
}
|
return {
|
||||||
return {
|
range: toRange(info.range),
|
||||||
range: toRange(info.range),
|
contents: toMarkedStringArray(info.contents),
|
||||||
contents: toMarkedStringArray(info.contents),
|
};
|
||||||
} as languages.Hover;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -400,17 +386,15 @@ export function createDocumentSymbolProvider(
|
||||||
getWorker: WorkerAccessor,
|
getWorker: WorkerAccessor,
|
||||||
): languages.DocumentSymbolProvider {
|
): languages.DocumentSymbolProvider {
|
||||||
return {
|
return {
|
||||||
provideDocumentSymbols(model) {
|
async provideDocumentSymbols(model) {
|
||||||
const resource = model.uri;
|
const resource = model.uri;
|
||||||
|
|
||||||
return getWorker(resource)
|
const worker = await getWorker(resource);
|
||||||
.then((worker) => worker.findDocumentSymbols(String(resource)))
|
const items = await worker.findDocumentSymbols(String(resource));
|
||||||
.then((items) => {
|
if (!items) {
|
||||||
if (!items) {
|
return;
|
||||||
return;
|
}
|
||||||
}
|
return items.map((item) => toDocumentSymbol(item));
|
||||||
return items.map((item) => toDocumentSymbol(item));
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -429,17 +413,15 @@ export function createDocumentFormattingEditProvider(
|
||||||
getWorker: WorkerAccessor,
|
getWorker: WorkerAccessor,
|
||||||
): languages.DocumentFormattingEditProvider {
|
): languages.DocumentFormattingEditProvider {
|
||||||
return {
|
return {
|
||||||
provideDocumentFormattingEdits(model, options) {
|
async provideDocumentFormattingEdits(model, options) {
|
||||||
const resource = model.uri;
|
const resource = model.uri;
|
||||||
|
|
||||||
return getWorker(resource).then((worker) =>
|
const worker = await getWorker(resource);
|
||||||
worker.format(String(resource), fromFormattingOptions(options)).then((edits) => {
|
const edits = await worker.format(String(resource), fromFormattingOptions(options));
|
||||||
if (!edits || edits.length === 0) {
|
if (!edits || edits.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return edits.map(toTextEdit);
|
return edits.map(toTextEdit);
|
||||||
}),
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -69,14 +69,10 @@ export function createWorkerManager(
|
||||||
stopWorker();
|
stopWorker();
|
||||||
},
|
},
|
||||||
|
|
||||||
getLanguageServiceWorker(...resources) {
|
async getLanguageServiceWorker(...resources) {
|
||||||
let _client: YAMLWorker;
|
const client = await getClient();
|
||||||
return getClient()
|
await worker.withSyncedResources(resources);
|
||||||
.then((client) => {
|
return client;
|
||||||
_client = client;
|
|
||||||
})
|
|
||||||
.then(() => worker.withSyncedResources(resources))
|
|
||||||
.then(() => _client);
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue