Skip to content

Commit

Permalink
Merge pull request #82 from Kingwl/inlayhints-support
Browse files Browse the repository at this point in the history
Add inlay hints support
  • Loading branch information
hediet authored Sep 6, 2021
2 parents 7903958 + 10e20ba commit 3ecea82
Show file tree
Hide file tree
Showing 15 changed files with 31,108 additions and 23,765 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules/
/out/
/release/
.DS_Store
16 changes: 16 additions & 0 deletions monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ declare namespace monaco.languages.typescript {
/** A full HTTP path to a JavaScript file which adds a function `customTSWorkerFactory` to the self inside a web-worker */
customWorkerPath?: string;
}
interface InlayHintsOptions {
readonly includeInlayParameterNameHints?: 'none' | 'literals' | 'all';
readonly includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean;
readonly includeInlayFunctionParameterTypeHints?: boolean;
readonly includeInlayVariableTypeHints?: boolean;
readonly includeInlayPropertyDeclarationTypeHints?: boolean;
readonly includeInlayFunctionLikeReturnTypeHints?: boolean;
readonly includeInlayEnumMemberValueHints?: boolean;
}
interface IExtraLib {
content: string;
version: number;
Expand Down Expand Up @@ -212,6 +221,7 @@ declare namespace monaco.languages.typescript {
*/
readonly onDidExtraLibsChange: IEvent<void>;
readonly workerOptions: WorkerOptions;
readonly inlayHintsOptions: InlayHintsOptions;
/**
* Get the current extra libs registered with the language service.
*/
Expand Down Expand Up @@ -413,6 +423,12 @@ declare namespace monaco.languages.typescript {
errorCodes: number[],
formatOptions: any
): Promise<ReadonlyArray<any>>;
/**
* Get inlay hints in the range of the file.
* @param fileName
* @returns `Promise<typescript.InlayHint[]>`
*/
provideInlayHints(fileName: string, start: number, end: number): Promise<ReadonlyArray<any>>;
}
export const typescriptVersion: string;
export const typescriptDefaults: LanguageServiceDefaults;
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"pretty-quick": "^3.1.0",
"requirejs": "^2.3.6",
"terser": "^5.6.0",
"typescript": "^4.3.2"
"typescript": "^4.4.2"
},
"husky": {
"hooks": {
Expand Down
62 changes: 54 additions & 8 deletions src/languageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ export class DiagnosticsAdapter extends Adapter {
}
};

this._disposables.push(editor.onDidCreateModel((model) => onModelAdd(<IInternalEditorModel>model)));
this._disposables.push(
editor.onDidCreateModel((model) => onModelAdd(<IInternalEditorModel>model))
);
this._disposables.push(editor.onWillDisposeModel(onModelRemoved));
this._disposables.push(
editor.onDidChangeModelLanguage((event) => {
Expand Down Expand Up @@ -574,9 +576,9 @@ function tagToString(tag: ts.JSDocTagInfo): string {
if (tag.name === 'param' && tag.text) {
const [paramName, ...rest] = tag.text;
tagLabel += `\`${paramName.text}\``;
if (rest.length > 0) tagLabel += ` — ${rest.map(r => r.text).join(' ')}`;
if (rest.length > 0) tagLabel += ` — ${rest.map((r) => r.text).join(' ')}`;
} else if (Array.isArray(tag.text)) {
tagLabel += ` — ${tag.text.map(r => r.text).join(' ')}`;
tagLabel += ` — ${tag.text.map((r) => r.text).join(' ')}`;
} else if (tag.text) {
tagLabel += ` — ${tag.text}`;
}
Expand Down Expand Up @@ -793,16 +795,14 @@ export class DefinitionAdapter extends Adapter {
range: this._textSpanToRange(refModel, entry.textSpan)
});
} else {
const matchedLibFile = typescriptDefaults.getExtraLibs()[entry.fileName]
const matchedLibFile = typescriptDefaults.getExtraLibs()[entry.fileName];
if (matchedLibFile) {
const libModel = editor.createModel(matchedLibFile.content, 'typescript', uri);
return {
uri: uri,
range: this._textSpanToRange(libModel, entry.textSpan)
}
};
}


}
}
return result;
Expand Down Expand Up @@ -894,7 +894,7 @@ export class OutlineAdapter extends Adapter implements languages.DocumentSymbolP
kind: <languages.SymbolKind>(outlineTypeTable[item.kind] || languages.SymbolKind.Variable),
range: this._textSpanToRange(model, item.spans[0]),
selectionRange: this._textSpanToRange(model, item.spans[0]),
tags: [],
tags: []
};

if (containerLabel) result.containerName = containerLabel;
Expand Down Expand Up @@ -1221,3 +1221,49 @@ export class RenameAdapter extends Adapter implements languages.RenameProvider {
return { edits };
}
}

// --- inlay hints ----

export class InlayHintsAdapter extends Adapter implements languages.InlayHintsProvider {
public async provideInlayHints(
model: editor.ITextModel,
range: Range,
token: CancellationToken
): Promise<languages.InlayHint[]> {
const resource = model.uri;
const fileName = resource.toString();
const start = model.getOffsetAt({
lineNumber: range.startLineNumber,
column: range.startColumn
});
const end = model.getOffsetAt({
lineNumber: range.endLineNumber,
column: range.endColumn
});
const worker = await this._worker(resource);
if (model.isDisposed()) {
return [];
}

const hints = await worker.provideInlayHints(fileName, start, end);

return hints.map((hint) => {
return {
...hint,
position: model.getPositionAt(hint.position),
kind: this._convertHintKind(hint.kind)
};
});
}

private _convertHintKind(kind?: ts.InlayHintKind) {
switch (kind) {
case 'Parameter':
return languages.InlayHintKind.Parameter;
case 'Type':
return languages.InlayHintKind.Type;
default:
return languages.InlayHintKind.Other;
}
}
}
20 changes: 10 additions & 10 deletions src/lib/lib.ts

Large diffs are not rendered by default.

Loading

0 comments on commit 3ecea82

Please sign in to comment.