@gdquest/codemirror-lsp
Version:
Enables Codemirror to interact with a local lsp, ie. a LSP that isn't socket-based, but rather work by function calls.
5 lines (4 loc) • 6.49 kB
JavaScript
var A=Object.create;var C=Object.defineProperty;var V=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var I=Object.getPrototypeOf,T=Object.prototype.hasOwnProperty;var j=(s,r)=>()=>(r||s((r={exports:{}}).exports,r),r.exports);var k=(s,r,e,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of N(r))!T.call(s,o)&&o!==e&&C(s,o,{get:()=>r[o],enumerable:!(t=V(r,o))||t.enumerable});return s};var B=(s,r,e)=>(e=s!=null?A(I(s)):{},k(r||!s||!s.__esModule?C(e,"default",{value:s,enumerable:!0}):e,s));var S=j((Q,_)=>{"use strict";var F=Object.prototype.hasOwnProperty,d="~";function R(){}Object.create&&(R.prototype=Object.create(null),new R().__proto__||(d=!1));function U(s,r,e){this.fn=s,this.context=r,this.once=e||!1}function q(s,r,e,t,o){if(typeof e!="function")throw new TypeError("The listener must be a function");var c=new U(e,t||s,o),i=d?d+r:r;return s._events[i]?s._events[i].fn?s._events[i]=[s._events[i],c]:s._events[i].push(c):(s._events[i]=c,s._eventsCount++),s}function x(s,r){--s._eventsCount===0?s._events=new R:delete s._events[r]}function m(){this._events=new R,this._eventsCount=0}m.prototype.eventNames=function(){var r=[],e,t;if(this._eventsCount===0)return r;for(t in e=this._events)F.call(e,t)&&r.push(d?t.slice(1):t);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(e)):r};m.prototype.listeners=function(r){var e=d?d+r:r,t=this._events[e];if(!t)return[];if(t.fn)return[t.fn];for(var o=0,c=t.length,i=new Array(c);o<c;o++)i[o]=t[o].fn;return i};m.prototype.listenerCount=function(r){var e=d?d+r:r,t=this._events[e];return t?t.fn?1:t.length:0};m.prototype.emit=function(r,e,t,o,c,i){var a=d?d+r:r;if(!this._events[a])return!1;var n=this._events[a],h=arguments.length,f,u;if(n.fn){switch(n.once&&this.removeListener(r,n.fn,void 0,!0),h){case 1:return n.fn.call(n.context),!0;case 2:return n.fn.call(n.context,e),!0;case 3:return n.fn.call(n.context,e,t),!0;case 4:return n.fn.call(n.context,e,t,o),!0;case 5:return n.fn.call(n.context,e,t,o,c),!0;case 6:return n.fn.call(n.context,e,t,o,c,i),!0}for(u=1,f=new Array(h-1);u<h;u++)f[u-1]=arguments[u];n.fn.apply(n.context,f)}else{var D=n.length,p;for(u=0;u<D;u++)switch(n[u].once&&this.removeListener(r,n[u].fn,void 0,!0),h){case 1:n[u].fn.call(n[u].context);break;case 2:n[u].fn.call(n[u].context,e);break;case 3:n[u].fn.call(n[u].context,e,t);break;case 4:n[u].fn.call(n[u].context,e,t,o);break;default:if(!f)for(p=1,f=new Array(h-1);p<h;p++)f[p-1]=arguments[p];n[u].fn.apply(n[u].context,f)}}return!0};m.prototype.on=function(r,e,t){return q(this,r,e,t,!1)};m.prototype.once=function(r,e,t){return q(this,r,e,t,!0)};m.prototype.removeListener=function(r,e,t,o){var c=d?d+r:r;if(!this._events[c])return this;if(!e)return x(this,c),this;var i=this._events[c];if(i.fn)i.fn===e&&(!o||i.once)&&(!t||i.context===t)&&x(this,c);else{for(var a=0,n=[],h=i.length;a<h;a++)(i[a].fn!==e||o&&!i[a].once||t&&i[a].context!==t)&&n.push(i[a]);n.length?this._events[c]=n.length===1?n[0]:n:x(this,c)}return this};m.prototype.removeAllListeners=function(r){var e;return r?(e=d?d+r:r,this._events[e]&&x(this,e)):(this._events=new R,this._eventsCount=0),this};m.prototype.off=m.prototype.removeListener;m.prototype.addListener=m.prototype.on;m.prefixed=d;m.EventEmitter=m;typeof _<"u"&&(_.exports=m)});import{linter as M,setDiagnostics as J}from"@codemirror/lint";import{ViewPlugin as K}from"@codemirror/view";var E=B(S(),1);var L=E.default;var w=class extends Error{constructor(e,t){let{cause:o,code:c,response:i}=t??{};super(e,{cause:o});this.code=0;this.response=null;c!=null&&(this.code=c),i!=null&&(this.response=i)}},P=class extends L{constructor(){super(...arguments);this._requests=new Map;this._lastId=0}async send(e){return await new Promise((t,o)=>{Array.isArray(e)?this._processBatch(e,t):this._process(e,t,o)})}notify(e){if(Array.isArray(e)){new Promise(t=>{this._processBatch(e,t)}).catch(t=>{console.error("Error while processing batch:",t)});return}new Promise((t,o)=>{this._process(e,t,o)}).catch(t=>{console.error("Error while running process:",t)})}_process(e,t,o){let c=this._lastId;this._lastId+=1;let i={jsonrpc:"2.0",id:c,...e},a=()=>{this.off("response",n),this.off("error",h)},n=f=>{f.id===c&&(t(f),a())},h=f=>{if(f.id===c){let u=new w(f.error.message,{code:f.error.code,response:f});o(u),a()}};this.on("response",n),this.on("error",h),this.emit("request",JSON.stringify(i))}_processBatch(e,t){let o=e.map(i=>{let a={jsonrpc:"2.0",id:this._lastId,...i};return this._lastId+=1,a}),c=i=>{let a=[];for(let n of i)for(let h of o)h.id===n.id&&a.push(n);a.length>0&&(t(a),this.off("batch",c))};this.on("batch",c),this.emit("request",o)}};function b(s,r){let e=0,t=0;for(let o=0;o<s.length;o++){if(s[o]===`
`?(e+=1,t=0):t+=1,r.line<e)return 0;if(e===r.line&&t===r.character)return o+1}return s.length}function X(s,r){let e=0,t=0;for(let o=1;o<s.length;o++)if(s[o-1]===`
`?(e+=1,t=0):t+=1,o===r)return{line:e,character:t};return{line:e,character:t}}function O(s){switch(s){case 1:return"error";case 2:return"warning";case 3:case 4:default:return"info"}}function se(s){let r=new E.default,e=null,t=0,{autocompletion:o=!0,onClientCommand:c,getData:i}=s,a=new P;class n{constructor(l){this.view=l}update(l){}}K.fromClass(n);let h=M(async p=>{e=p,r.emit("view",p);let l=[],g=t;return t+=1,a.notify({method:"textDocument/didChange",params:{textDocument:{uri:"file:///whatever",version:g},contentChanges:[{text:p.state.doc.toString()}]}}),l},{});a.on("request",p=>{c(p)});let f=p=>{switch(p.method){case"textDocument/publishDiagnostics":u(p);break}};a.on("notification",f);let u=p=>{if(console.log("onPublishDiagnostics",p,e),e==null)return;let l=p.params.diagnostics.map(y=>({from:b(e?.state.doc.toString()??"",y.range.start),to:b(e?.state.doc.toString()??"",y.range.end),severity:O(y.severity),source:y.source,message:y.message}));console.log("diagnostics",l);let g=J(e.state,l),v=e.state.update(g);e.dispatch(v)};return i({emitServerCommand:p=>{let l=JSON.parse(p);if(typeof l=="string")throw new Error("JSON is string");if(Array.isArray(l)){let g=[];for(let v of l)"method"in v?a.emit("notification",v):g.push(v);a.emit("batch",g);return}if("error"in l){a.emit("error",l);return}if("result"in l){a.emit("response",l);return}if("method"in l){a.emit("notification",l);return}throw new Error("unknown error")}}),[h,...o?[]:[]]}export{se as createLsp,b as getCharacterFromPosition,O as getDiagnosticSeverity,X as getPositionFromCharacter};
//# sourceMappingURL=index.min.mjs.map