[mod] improve formatting
This commit is contained in:
parent
0508901f72
commit
877e7b8d13
5 changed files with 47 additions and 17 deletions
97
bundles/cm6/lsp.js
Normal file
97
bundles/cm6/lsp.js
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
|
||||
import { EditorState, StateEffect } from '@codemirror/state';
|
||||
|
||||
import { lineNumbers, highlightActiveLineGutter, highlightSpecialChars,
|
||||
drawSelection, rectangularSelection, crosshairCursor, highlightActiveLine,
|
||||
keymap, dropCursor,EditorView} from '@codemirror/view';
|
||||
|
||||
import { openSearchPanel, highlightSelectionMatches, searchKeymap } from '@codemirror/search';
|
||||
|
||||
import { openLintPanel, lintGutter, lintKeymap, linter} from "@codemirror/lint"
|
||||
|
||||
import { indentWithTab, history, defaultKeymap, historyKeymap } from '@codemirror/commands';
|
||||
|
||||
import { foldGutter, indentOnInput, indentUnit, bracketMatching, foldKeymap,
|
||||
syntaxHighlighting, defaultHighlightStyle , StreamLanguage } from '@codemirror/language';
|
||||
|
||||
import { closeBrackets, autocompletion, closeBracketsKeymap, completionKeymap } from '@codemirror/autocomplete';
|
||||
|
||||
import { LSPClient, LSPPlugin, languageServerSupport, languageServerExtensions,
|
||||
formatDocument,formatKeymap } from "@codemirror/lsp-client";
|
||||
|
||||
import { xQuery } from "@codemirror/legacy-modes/mode/xquery"
|
||||
|
||||
|
||||
|
||||
// Language
|
||||
import { xml } from "@codemirror/lang-xml";
|
||||
|
||||
// return promise with socket map or reject if no connect
|
||||
function simpleWebSocketTransport(uri) {
|
||||
let handlers = [];
|
||||
return new Promise(function (resolve, reject) {
|
||||
let sock = new WebSocket(uri);
|
||||
|
||||
sock.onmessage = e => { for (let h of handlers) h(e.data.toString()); };
|
||||
sock.onerror = e => reject(e);
|
||||
sock.onopen = () => resolve({
|
||||
socket: sock,
|
||||
send: (message) => sock.send(message),
|
||||
subscribe: (handler) => handlers.push(handler),
|
||||
unsubscribe: (handler) => handlers = handlers.filter(h => h != handler)
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
const baseExts = [
|
||||
lineNumbers(),
|
||||
highlightActiveLineGutter(),
|
||||
highlightSpecialChars(),
|
||||
history(),
|
||||
foldGutter(),
|
||||
lintGutter(),
|
||||
drawSelection(),
|
||||
dropCursor(),
|
||||
EditorState.allowMultipleSelections.of(true),
|
||||
EditorView.lineWrapping,
|
||||
indentOnInput(),
|
||||
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
|
||||
bracketMatching(),
|
||||
closeBrackets(),
|
||||
autocompletion(),
|
||||
rectangularSelection(),
|
||||
crosshairCursor(),
|
||||
highlightActiveLine(),
|
||||
highlightSelectionMatches(),
|
||||
keymap.of([
|
||||
...closeBracketsKeymap,
|
||||
...defaultKeymap,
|
||||
...searchKeymap,
|
||||
...historyKeymap,
|
||||
...foldKeymap,
|
||||
...completionKeymap,
|
||||
...lintKeymap
|
||||
]),
|
||||
StreamLanguage.define(xQuery)
|
||||
];
|
||||
|
||||
|
||||
|
||||
// map cmd->{keybings,fn}
|
||||
function listCommands(view) {
|
||||
const commands = new Map();
|
||||
const keymaps = view.state.facet(keymap);
|
||||
for (let km of keymaps) {
|
||||
for (let binding of km) {
|
||||
if (binding.run && binding.run.name) {
|
||||
commands.set(binding.run.name, { key: binding.key, fn: binding.run });
|
||||
}
|
||||
}
|
||||
}
|
||||
return commands;
|
||||
};
|
||||
|
||||
export { baseExts, EditorView, EditorState, StateEffect, LSPPlugin, LSPClient,
|
||||
openSearchPanel, openLintPanel, languageServerSupport, languageServerExtensions,
|
||||
simpleWebSocketTransport, linter, formatDocument,keymap,formatKeymap, listCommands };
|
||||
41
bundles/cm6/try.js
Normal file
41
bundles/cm6/try.js
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import {basicSetup, EditorView} from "codemirror"
|
||||
import { EditorState } from '@codemirror/state';
|
||||
import { LSPClient, LSPPlugin, languageServerSupport } from "@codemirror/lsp-client";
|
||||
import {StreamLanguage} from "@codemirror/language"
|
||||
import { xQuery } from "@codemirror/legacy-modes/mode/xquery"
|
||||
|
||||
function simpleWebSocketTransport(uri) {
|
||||
let handlers = [];
|
||||
return new Promise(function (resolve, reject) {
|
||||
let sock = new WebSocket(uri);
|
||||
|
||||
sock.onmessage = e => { for (let h of handlers) h(e.data.toString()); };
|
||||
sock.onerror = e => reject(e);
|
||||
sock.onopen = () => resolve({
|
||||
socket: sock,
|
||||
send: (message) => sock.send(message),
|
||||
subscribe: (handler) => handlers.push(handler),
|
||||
unsubscribe: (handler) => handlers = handlers.filter(h => h != handler)
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
simpleWebSocketTransport("ws://localhost:3000/ws/lsp")
|
||||
.then(transport => {
|
||||
|
||||
client = new LSPClient().connect(transport);
|
||||
|
||||
let extLsp = languageServerSupport(client, file, "xquery");
|
||||
|
||||
const doc = view.state.doc.toString();
|
||||
const state = lsp.createEditorState(doc, [...lsp.baseExts, extLsp, extLint]);
|
||||
view.setState(state);
|
||||
})
|
||||
.catch(r => { alert("connection failed: " + server) });
|
||||
|
||||
|
||||
new EditorView({
|
||||
doc: "xquery version '3.1';\n(:~ comment:)\nmodule namespace pdfbox='ns';\n",
|
||||
extensions: [basicSetup, StreamLanguage.define(xQuery)],
|
||||
parent: document.body
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue