PluginProbe
Code Block Pro – Beautiful Syntax Highlighting / 1.27.5
Code Block Pro – Beautiful Syntax Highlighting v1.27.5
1.27.1 1.27.2 1.27.3 1.27.4 1.27.5 1.27.6 1.27.7 1.28.0 1.3.0 1.4.0 1.5.0 1.5.1 1.5.2 1.6.0 1.7.0 1.8.0 1.9.0 1.9.1 1.9.2 1.9.3 trunk 1.1.0 1.10.0 1.11.0 1.11.1 All 63 releases
code-block-pro / build / shiki / node_modules / jsonc-parser / lib / esm / impl / format.js

format.js in Code Block Pro – Beautiful Syntax Highlighting 1.27.5, at build/shiki/node_modules/jsonc-parser/lib/esm/impl/format.js

1 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 "use strict";import{createScanner}from"./scanner";import{cachedSpaces,cachedBreakLinesWithSpaces,supportedEols}from"./string-intern";export function format(e,t,n){let c,s,a,r,i;if(t){for(r=t.offset,i=r+t.length,a=r;a>0&&!isEOL(e,a-1);)a--;let p=i;for(;p<e.length&&!isEOL(e,p);)p++;s=e.substring(a,p),c=computeIndentLevel(s,n)}else s=e,c=0,a=0,r=0,i=e.length;const p=getEOL(n,e),o=supportedEols.includes(p);let f,h=0,l=0;f=n.insertSpaces?cachedSpaces[n.tabSize||4]??repeat(cachedSpaces[1],n.tabSize||4):"\t";const k="\t"===f?"\t":" ";let L=createScanner(s,!1),u=!1;function g(){if(h>1)return repeat(p,h)+repeat(f,c+l);const e=f.length*(c+l);return!o||e>cachedBreakLinesWithSpaces[k][p].length?p+repeat(f,c+l):e<=0?p:cachedBreakLinesWithSpaces[k][p][e]}function S(){let e=L.scan();for(h=0;15===e||14===e;)14===e&&n.keepLines?h+=1:14===e&&(h=1),e=L.scan();return u=16===e||0!==L.getTokenError(),e}const d=[];function b(n,c,s){u||t&&!(c<i&&s>r)||e.substring(c,s)===n||d.push({offset:c,length:s-c,content:n})}let O=S();if(n.keepLines&&h>0&&b(repeat(p,h),0,0),17!==O){let e=L.getTokenOffset()+a;b(f.length*c<20&&n.insertSpaces?cachedSpaces[f.length*c]:repeat(f,c),a,e)}for(;17!==O;){let e=L.getTokenOffset()+L.getTokenLength()+a,t=S(),c="",s=!1;for(;0===h&&(12===t||13===t);){let n=L.getTokenOffset()+a;b(cachedSpaces[1],e,n),e=L.getTokenOffset()+L.getTokenLength()+a,s=12===t,c=s?g():"",t=S()}if(2===t)1!==O&&l--,n.keepLines&&h>0||!n.keepLines&&1!==O?c=g():n.keepLines&&(c=cachedSpaces[1]);else if(4===t)3!==O&&l--,n.keepLines&&h>0||!n.keepLines&&3!==O?c=g():n.keepLines&&(c=cachedSpaces[1]);else{switch(O){case 3:case 1:l++,c=n.keepLines&&h>0||!n.keepLines?g():cachedSpaces[1];break;case 5:c=n.keepLines&&h>0||!n.keepLines?g():cachedSpaces[1];break;case 12:c=g();break;case 13:h>0?c=g():s||(c=cachedSpaces[1]);break;case 6:n.keepLines&&h>0?c=g():s||(c=cachedSpaces[1]);break;case 10:n.keepLines&&h>0?c=g():6!==t||s||(c="");break;case 7:case 8:case 9:case 11:case 2:case 4:n.keepLines&&h>0?c=g():12!==t&&13!==t||s?5!==t&&17!==t&&(u=!0):c=cachedSpaces[1];break;case 16:u=!0}h>0&&(12===t||13===t)&&(c=g())}17===t&&(c=n.keepLines&&h>0?g():n.insertFinalNewline?p:""),b(c,e,L.getTokenOffset()+a),O=t}return d}function repeat(e,t){let n="";for(let c=0;c<t;c++)n+=e;return n}function computeIndentLevel(e,t){let n=0,c=0;const s=t.tabSize||4;for(;n<e.length;){let t=e.charAt(n);if(t===cachedSpaces[1])c++;else{if("\t"!==t)break;c+=s}n++}return Math.floor(c/s)}function getEOL(e,t){for(let e=0;e<t.length;e++){const n=t.charAt(e);if("\r"===n)return e+1<t.length&&"\n"===t.charAt(e+1)?"\r\n":"\r";if("\n"===n)return"\n"}return e&&e.eol||"\n"}export function isEOL(e,t){return-1!=="\r\n".indexOf(e.charAt(t))}