[add] diags from server

This commit is contained in:
Andy Bunce 2025-08-14 18:20:41 +01:00
parent 5c759eb3f4
commit 2078055d05
8 changed files with 150 additions and 15 deletions

View file

@ -16730,6 +16730,16 @@ var lsp = (function (exports) {
return state.field(lintState, false) ? effects : effects.concat(StateEffect.appendConfig.of(lintExtensions));
}
/**
Returns a transaction spec which updates the current set of
diagnostics, and enables the lint extension if if wasn't already
active.
*/
function setDiagnostics(state, diagnostics) {
return {
effects: maybeEnableLint(state, [setDiagnosticsEffect.of(diagnostics)])
};
}
/**
The state effect that updates the set of active diagnostics. Can
be useful when writing an extension that needs to track these.
*/
@ -16846,6 +16856,65 @@ var lsp = (function (exports) {
{ key: "Mod-Shift-m", run: openLintPanel, preventDefault: true },
{ key: "F8", run: nextDiagnostic }
];
const lintPlugin = /*@__PURE__*/ViewPlugin.fromClass(class {
constructor(view) {
this.view = view;
this.timeout = -1;
this.set = true;
let { delay } = view.state.facet(lintConfig);
this.lintTime = Date.now() + delay;
this.run = this.run.bind(this);
this.timeout = setTimeout(this.run, delay);
}
run() {
clearTimeout(this.timeout);
let now = Date.now();
if (now < this.lintTime - 10) {
this.timeout = setTimeout(this.run, this.lintTime - now);
}
else {
this.set = false;
let { state } = this.view, { sources } = state.facet(lintConfig);
if (sources.length)
batchResults(sources.map(s => Promise.resolve(s(this.view))), annotations => {
if (this.view.state.doc == state.doc)
this.view.dispatch(setDiagnostics(this.view.state, annotations.reduce((a, b) => a.concat(b))));
}, error => { logException(this.view.state, error); });
}
}
update(update) {
let config = update.state.facet(lintConfig);
if (update.docChanged || config != update.startState.facet(lintConfig) ||
config.needsRefresh && config.needsRefresh(update)) {
this.lintTime = Date.now() + config.delay;
if (!this.set) {
this.set = true;
this.timeout = setTimeout(this.run, config.delay);
}
}
}
force() {
if (this.set) {
this.lintTime = Date.now();
this.run();
}
}
destroy() {
clearTimeout(this.timeout);
}
});
function batchResults(promises, sink, error) {
let collected = [], timeout = -1;
for (let p of promises)
p.then(value => {
collected.push(value);
clearTimeout(timeout);
if (collected.length == promises.length)
sink(collected);
else
timeout = setTimeout(() => sink(collected), 200);
}, error);
}
const lintConfig = /*@__PURE__*/Facet.define({
combine(input) {
return Object.assign({ sources: input.map(i => i.source).filter(x => x != null) }, combineConfig(input.map(i => i.config), {
@ -16859,6 +16928,19 @@ var lsp = (function (exports) {
}));
}
});
/**
Given a diagnostic source, this function returns an extension that
enables linting with that source. It will be called whenever the
editor is idle (after its content changed). If `null` is given as
source, this only configures the lint extension.
*/
function linter(source, config = {}) {
return [
lintConfig.of({ source, config }),
lintPlugin,
lintExtensions
];
}
function assignKeys(actions) {
let assigned = [];
if (actions)
@ -31357,13 +31439,16 @@ ${text}</tr>
return new EditorView({ state, parent });
}
exports.LSPPlugin = LSPPlugin;
exports.baseExts = baseExts;
exports.client = client;
exports.createEditorState = createEditorState;
exports.createEditorView = createEditorView;
exports.languageServerSupport = languageServerSupport;
exports.linter = linter;
exports.openLintPanel = openLintPanel;
exports.openSearchPanel = openSearchPanel;
exports.setDiagnostics = setDiagnostics;
exports.simpleWebSocketTransport = simpleWebSocketTransport;
return exports;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -65,6 +65,7 @@ function connect() {
const file = document.getElementById("iFile").value;
lsp.simpleWebSocketTransport(server)
.then(transport => {
transport.subscribe(incoming);
client = lsp.client(transport);
let plugin=lsp.languageServerSupport(client,file,"xquery")
const doc = view.state.doc.toString();
@ -73,3 +74,46 @@ function connect() {
})
.catch(r => alert("connection failed: "+ server));
};
function incoming(msg) {
const rpc=JSON.parse(msg)
switch (rpc.method) {
case "textDocument/publishDiagnostics":
diags(rpc.params);
break;
default:
return;
}
};
function diags(params){
console.log("--",params)
let plugin= lsp.LSPPlugin.get(view)
const diagnostics = params.diagnostics
.map(({ range, message, severity }) => ({
from: plugin.fromPosition( range.start,view.state.doc),
to: plugin.fromPosition( range.end,view.state.doc),
severity: 'error',
message,
}))
.filter(
({ from, to }) =>
from !== null &&
to !== null &&
from !== undefined &&
to !== undefined,
)
.sort((a, b) => {
switch (true) {
case a.from < b.from:
return -1;
case a.from > b.from:
return 1;
}
return 0;
});
view.dispatch(lsp.setDiagnostics(view.state, diagnostics));
};