Guest User

Untitled

a guest
Oct 17th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. import {
  2. createConnection,
  3. TextDocuments,
  4. TextDocument,
  5. Diagnostic,
  6. DiagnosticSeverity,
  7. ProposedFeatures,
  8. InitializeParams,
  9. DidChangeConfigurationNotification,
  10. CompletionItem,
  11. CompletionItemKind,
  12. TextDocumentPositionParams
  13. } from 'vscode-languageserver';
  14.  
  15. // Create a connection for the server. The connection uses Node's IPC as a transport.
  16. // Also include all preview / proposed LSP features.
  17. let connection = createConnection(ProposedFeatures.all);
  18.  
  19. // Create a simple text document manager. The text document manager
  20. // supports full document sync only
  21. let documents: TextDocuments = new TextDocuments();
  22.  
  23. let hasConfigurationCapability: boolean = false;
  24. let hasWorkspaceFolderCapability: boolean = false;
  25. let hasDiagnosticRelatedInformationCapability: boolean = false;
  26.  
  27. connection.onInitialize((params: InitializeParams) => {
  28. let capabilities = params.capabilities;
  29.  
  30. // Does the client support the `workspace/configuration` request?
  31. // If not, we will fall back using global settings
  32. hasConfigurationCapability =
  33. capabilities.workspace && !!capabilities.workspace.configuration;
  34. hasWorkspaceFolderCapability =
  35. capabilities.workspace && !!capabilities.workspace.workspaceFolders;
  36. hasDiagnosticRelatedInformationCapability =
  37. capabilities.textDocument &&
  38. capabilities.textDocument.publishDiagnostics &&
  39. capabilities.textDocument.publishDiagnostics.relatedInformation;
  40.  
  41. return {
  42. capabilities: {
  43. textDocumentSync: documents.syncKind,
  44. // Tell the client that the server supports code completion
  45. completionProvider: {
  46. resolveProvider: true
  47. }
  48. }
  49. };
  50. });
  51.  
  52. connection.onInitialized(() => {
  53. if (hasConfigurationCapability) {
  54. // Register for all configuration changes.
  55. connection.client.register(
  56. DidChangeConfigurationNotification.type,
  57. undefined
  58. );
  59. }
  60. if (hasWorkspaceFolderCapability) {
  61. connection.workspace.onDidChangeWorkspaceFolders(_event => {
  62. connection.console.log('Workspace folder change event received.');
  63. });
  64. }
  65. });
  66.  
  67. // The example settings
  68. interface ExampleSettings {
  69. maxNumberOfProblems: number;
  70. }
  71.  
  72. // The global settings, used when the `workspace/configuration` request is not supported by the client.
  73. // Please note that this is not the case when using this server with the client provided in this example
  74. // but could happen with other clients.
  75. const defaultSettings: ExampleSettings = { maxNumberOfProblems: 1000 };
  76. let globalSettings: ExampleSettings = defaultSettings;
  77.  
  78. // Cache the settings of all open documents
  79. let documentSettings: Map<string, Thenable<ExampleSettings>> = new Map();
  80.  
  81. connection.onDidChangeConfiguration(change => {
  82. if (hasConfigurationCapability) {
  83. // Reset all cached document settings
  84. documentSettings.clear();
  85. } else {
  86. globalSettings = <ExampleSettings>(
  87. (change.settings.languageServerExample || defaultSettings)
  88. );
  89. }
  90.  
  91. // Revalidate all open text documents
  92. documents.all().forEach(validateTextDocument);
  93. });
  94.  
  95. function getDocumentSettings(resource: string): Thenable<ExampleSettings> {
  96. if (!hasConfigurationCapability) {
  97. return Promise.resolve(globalSettings);
  98. }
  99. let result = documentSettings.get(resource);
  100. if (!result) {
  101. result = connection.workspace.getConfiguration({
  102. scopeUri: resource,
  103. section: 'languageServerExample'
  104. });
  105. documentSettings.set(resource, result);
  106. }
  107. return result;
  108. }
  109.  
  110. // Only keep settings for open documents
  111. documents.onDidClose(e => {
  112. documentSettings.delete(e.document.uri);
  113. });
  114.  
  115. // The content of a text document has changed. This event is emitted
  116. // when the text document first opened or when its content has changed.
  117. documents.onDidChangeContent(change => {
  118. validateTextDocument(change.document);
  119. });
  120.  
  121. async function validateTextDocument(textDocument: TextDocument): Promise<void> {
  122. // In this simple example we get the settings for every validate run.
  123. let settings = await getDocumentSettings(textDocument.uri);
  124.  
  125. // The validator creates diagnostics for all uppercase words length 2 and more
  126. let text = textDocument.getText();
  127. let pattern = /\b[A-Z]{2,}\b/g;
  128. let m: RegExpExecArray;
  129.  
  130. let problems = 0;
  131. let diagnostics: Diagnostic[] = [];
  132. while ((m = pattern.exec(text)) && problems < settings.maxNumberOfProblems) {
  133. problems++;
  134. let diagnostic: Diagnostic = {
  135. severity: DiagnosticSeverity.Warning,
  136. range: {
  137. start: textDocument.positionAt(m.index),
  138. end: textDocument.positionAt(m.index + m[0].length)
  139. },
  140. message: `${m[0]} is all uppercase.`,
  141. source: 'ex'
  142. };
  143. if (hasDiagnosticRelatedInformationCapability) {
  144. diagnostic.relatedInformation = [
  145. {
  146. location: {
  147. uri: textDocument.uri,
  148. range: Object.assign({}, diagnostic.range)
  149. },
  150. message: 'Spelling matters'
  151. },
  152. {
  153. location: {
  154. uri: textDocument.uri,
  155. range: Object.assign({}, diagnostic.range)
  156. },
  157. message: 'Particularly for names'
  158. }
  159. ];
  160. }
  161. diagnostics.push(diagnostic);
  162. }
  163.  
  164. // Send the computed diagnostics to VSCode.
  165. connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
  166. }
  167.  
  168. connection.onDidChangeWatchedFiles(_change => {
  169. // Monitored files have change in VSCode
  170. connection.console.log('We received an file change event');
  171. });
  172.  
  173. // This handler provides the initial list of the completion items.
  174. connection.onCompletion(
  175. (_textDocumentPosition: TextDocumentPositionParams): CompletionItem[] => {
  176. // The pass parameter contains the position of the text document in
  177. // which code complete got requested. For the example we ignore this
  178. // info and always provide the same completion items.
  179. return [
  180. {
  181. label: 'TypeScript',
  182. kind: CompletionItemKind.Text,
  183. data: 1
  184. },
  185. {
  186. label: 'JavaScript',
  187. kind: CompletionItemKind.Text,
  188. data: 2
  189. }
  190. ];
  191. }
  192. );
  193.  
  194. // This handler resolves additional information for the item selected in
  195. // the completion list.
  196. connection.onCompletionResolve(
  197. (item: CompletionItem): CompletionItem => {
  198. if (item.data === 1) {
  199. item.detail = 'TypeScript details';
  200. item.documentation = 'TypeScript documentation';
  201. } else if (item.data === 2) {
  202. item.detail = 'JavaScript details';
  203. item.documentation = 'JavaScript documentation';
  204. }
  205. return item;
  206. }
  207. );
  208.  
  209.  
  210. /*
  211. connection.onDidOpenTextDocument((params) => {
  212. // A text document got opened in VSCode.
  213. // params.uri uniquely identifies the document. For documents store on disk this is a file URI.
  214. // params.text the initial full content of the document.
  215. connection.console.log(`${params.textDocument.uri} opened.`);
  216. });
  217. connection.onDidChangeTextDocument((params) => {
  218. // The content of a text document did change in VSCode.
  219. // params.uri uniquely identifies the document.
  220. // params.contentChanges describe the content changes to the document.
  221. connection.console.log(`${params.textDocument.uri} changed: ${JSON.stringify(params.contentChanges)}`);
  222. });
  223. connection.onDidCloseTextDocument((params) => {
  224. // A text document got closed in VSCode.
  225. // params.uri uniquely identifies the document.
  226. connection.console.log(`${params.textDocument.uri} closed.`);
  227. });
  228. */
  229.  
  230. // Make the text document manager listen on the connection
  231. // for open, change and close text document events
  232. documents.listen(connection);
  233.  
  234. // Listen on the connection
  235. connection.listen();
Add Comment
Please, Sign In to add comment