Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5ceb78db72 |
No files matched your search
@@ -94,6 +94,7 @@ monaco.defineTheme("ak-dark", OneDark);
|
||||
*/
|
||||
const LanguageServices: Partial<Record<CodeEditorMode, () => Promise<unknown>>> = {
|
||||
[CodeEditorMode.CSS]: () => import("#elements/CodeEditor/languages/css-service"),
|
||||
[CodeEditorMode.Python]: () => import("#elements/CodeEditor/languages/python-service"),
|
||||
};
|
||||
|
||||
function loadLanguageService(mode: CodeEditorMode): void {
|
||||
@@ -102,6 +103,13 @@ function loadLanguageService(mode: CodeEditorMode): void {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Python is indented by four spaces by convention, and in authentik's documentation.
|
||||
*/
|
||||
function tabSize(mode: CodeEditorMode): number {
|
||||
return mode === CodeEditorMode.Python ? 4 : 2;
|
||||
}
|
||||
|
||||
function themeName(theme: ResolvedUITheme): string {
|
||||
return theme === UiThemeEnum.Dark ? "ak-dark" : "ak-light";
|
||||
}
|
||||
@@ -146,7 +154,7 @@ export class MonacoEditor implements Disposable {
|
||||
automaticLayout: true,
|
||||
minimap: { enabled: false },
|
||||
wordWrap: "on",
|
||||
tabSize: 2,
|
||||
tabSize: tabSize(mode),
|
||||
// The editor grows with its content, see the content size listener below.
|
||||
scrollBeyondLastLine: false,
|
||||
// Let the page scroll once the editor has nothing left to scroll.
|
||||
@@ -194,7 +202,10 @@ export class MonacoEditor implements Disposable {
|
||||
public setMode(mode: CodeEditorMode): void {
|
||||
const model = this.editor.getModel();
|
||||
|
||||
if (model) monaco.setModelLanguage(model, mode);
|
||||
if (model) {
|
||||
monaco.setModelLanguage(model, mode);
|
||||
model.updateOptions({ tabSize: tabSize(mode) });
|
||||
}
|
||||
|
||||
loadLanguageService(mode);
|
||||
}
|
||||
|
||||
@@ -65,3 +65,12 @@ declare module "monaco-editor/languages/features/css/cssWorker" {
|
||||
constructor(ctx: CSSWorkerContext, createData: CSSWorkerCreateData);
|
||||
}
|
||||
}
|
||||
|
||||
declare module "monaco-editor/languages/definitions/python/python" {
|
||||
import type { languages } from "monaco-editor/editor/editor.api";
|
||||
|
||||
/**
|
||||
* The Monarch definition Monaco highlights Python with.
|
||||
*/
|
||||
export const language: languages.IMonarchLanguage & { keywords: string[] };
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* @remarks
|
||||
* Expressions are the body of a function the server wraps them in, so `return` is valid at the
|
||||
* top level and the context variables are that function's parameters, which are not known here.
|
||||
* @file Python suggestions.
|
||||
*/
|
||||
|
||||
import { editor, languages, Range } from "monaco-editor/editor/editor.api";
|
||||
import { language } from "monaco-editor/languages/definitions/python/python";
|
||||
|
||||
/**
|
||||
* The keywords and builtins Monaco highlights, without dunder names such as `__init__`.
|
||||
*/
|
||||
const Keywords = Array.from(new Set(language.keywords as string[])).filter(
|
||||
(keyword) => !keyword.startsWith("__"),
|
||||
);
|
||||
|
||||
const KeywordSet = new Set(Keywords);
|
||||
|
||||
const Snippets: Record<string, string> = {
|
||||
"if": "if ${1:condition}:\n\t${0:pass}",
|
||||
"if/else": "if ${1:condition}:\n\t${2:pass}\nelse:\n\t${0:pass}",
|
||||
"for": "for ${1:item} in ${2:items}:\n\t${0:pass}",
|
||||
"try/except": "try:\n\t${1:pass}\nexcept ${2:Exception} as ${3:exc}:\n\t${0:pass}",
|
||||
"def": "def ${1:name}(${2}):\n\t${0:pass}",
|
||||
"with": "with ${1:expression} as ${2:target}:\n\t${0:pass}",
|
||||
};
|
||||
|
||||
/**
|
||||
* Names already written in the expression, as Monaco's tokenizer finds them, so names in strings
|
||||
* and comments are left out.
|
||||
*/
|
||||
function collectNames(model: editor.ITextModel, currentWord: string): Set<string> {
|
||||
const names = new Set<string>();
|
||||
const lines = model.getLinesContent();
|
||||
|
||||
editor.tokenize(model.getValue(), "python").forEach((tokens, lineIndex) => {
|
||||
const line = lines[lineIndex];
|
||||
|
||||
tokens.forEach((token, tokenIndex) => {
|
||||
if (token.type !== "identifier.python") return;
|
||||
|
||||
const end = tokens[tokenIndex + 1]?.offset ?? line.length;
|
||||
const name = line.slice(token.offset, end);
|
||||
|
||||
if (name !== currentWord && !KeywordSet.has(name)) names.add(name);
|
||||
});
|
||||
});
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
languages.registerCompletionItemProvider("python", {
|
||||
provideCompletionItems(model, position) {
|
||||
const word = model.getWordUntilPosition(position);
|
||||
|
||||
const range = new Range(
|
||||
position.lineNumber,
|
||||
word.startColumn,
|
||||
position.lineNumber,
|
||||
word.endColumn,
|
||||
);
|
||||
|
||||
// Attribute access needs to know the object's type.
|
||||
const before = model.getLineContent(position.lineNumber).slice(0, word.startColumn - 1);
|
||||
|
||||
if (before.trimEnd().endsWith(".")) return { suggestions: [] };
|
||||
|
||||
const { CompletionItemKind: Kind, CompletionItemInsertTextRule: Rule } = languages;
|
||||
|
||||
const suggestions: languages.CompletionItem[] = [
|
||||
...Keywords.map((label) => ({
|
||||
label,
|
||||
kind: Kind.Keyword,
|
||||
insertText: label,
|
||||
range,
|
||||
})),
|
||||
...Object.entries(Snippets).map(([label, insertText]) => ({
|
||||
label,
|
||||
kind: Kind.Snippet,
|
||||
insertText,
|
||||
insertTextRules: Rule.InsertAsSnippet,
|
||||
range,
|
||||
})),
|
||||
...Array.from(collectNames(model, word.word), (label) => ({
|
||||
label,
|
||||
kind: Kind.Variable,
|
||||
insertText: label,
|
||||
range,
|
||||
})),
|
||||
];
|
||||
|
||||
return { suggestions };
|
||||
},
|
||||
});
|
||||
Reference in new issue
Block a user