@astrojs/language-server 0.12.0 → 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @astrojs/language-server
2
2
 
3
+ ## 0.12.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 49955c6: Add support for colors indicators and color picker
8
+
3
9
  ## 0.12.0
4
10
 
5
11
  ### Minor Changes
@@ -1,4 +1,4 @@
1
- import { CancellationToken, CompletionContext, CompletionItem, CompletionList, DefinitionLink, Diagnostic, FoldingRange, Hover, Position, Location, SignatureHelp, SignatureHelpContext, TextDocumentContentChangeEvent, TextDocumentIdentifier, WorkspaceEdit } from 'vscode-languageserver';
1
+ import { CancellationToken, Color, ColorInformation, ColorPresentation, CompletionContext, CompletionItem, CompletionList, DefinitionLink, Diagnostic, FoldingRange, Hover, Position, Range, Location, SignatureHelp, SignatureHelpContext, TextDocumentContentChangeEvent, TextDocumentIdentifier, WorkspaceEdit } from 'vscode-languageserver';
2
2
  import type { AppCompletionItem, Plugin } from './interfaces';
3
3
  import { DocumentManager } from '../core/documents/DocumentManager';
4
4
  interface PluginHostConfig {
@@ -20,6 +20,8 @@ export declare class PluginHost {
20
20
  getFoldingRanges(textDocument: TextDocumentIdentifier): Promise<FoldingRange[] | null>;
21
21
  getDefinitions(textDocument: TextDocumentIdentifier, position: Position): Promise<DefinitionLink[] | Location[]>;
22
22
  rename(textDocument: TextDocumentIdentifier, position: Position, newName: string): Promise<WorkspaceEdit | null>;
23
+ getDocumentColors(textDocument: TextDocumentIdentifier): Promise<ColorInformation[]>;
24
+ getColorPresentations(textDocument: TextDocumentIdentifier, range: Range, color: Color): Promise<ColorPresentation[]>;
23
25
  getSignatureHelp(textDocument: TextDocumentIdentifier, position: Position, context: SignatureHelpContext | undefined, cancellationToken: CancellationToken): Promise<SignatureHelp | null>;
24
26
  onWatchFileChanges(onWatchFileChangesParams: any[]): void;
25
27
  updateNonAstroFile(fileName: string, changes: TextDocumentContentChangeEvent[]): void;
@@ -67,6 +67,14 @@ class PluginHost {
67
67
  const document = this.getDocument(textDocument.uri);
68
68
  return this.execute('rename', [document, position, newName], ExecuteMode.FirstNonNull);
69
69
  }
70
+ async getDocumentColors(textDocument) {
71
+ const document = this.getDocument(textDocument.uri);
72
+ return (0, lodash_1.flatten)(await this.execute('getDocumentColors', [document], ExecuteMode.Collect));
73
+ }
74
+ async getColorPresentations(textDocument, range, color) {
75
+ const document = this.getDocument(textDocument.uri);
76
+ return (0, lodash_1.flatten)(await this.execute('getColorPresentations', [document, range, color], ExecuteMode.Collect));
77
+ }
70
78
  async getSignatureHelp(textDocument, position, context, cancellationToken) {
71
79
  const document = this.getDocument(textDocument.uri);
72
80
  if (!document) {
@@ -1,4 +1,4 @@
1
- import { CompletionContext, CompletionList, Position } from 'vscode-languageserver';
1
+ import { Color, ColorInformation, ColorPresentation, CompletionContext, CompletionList, Position, Range } from 'vscode-languageserver';
2
2
  import { ConfigManager } from '../../core/config/ConfigManager';
3
3
  import { AstroDocument } from '../../core/documents';
4
4
  import type { Plugin } from '../interfaces';
@@ -10,6 +10,8 @@ export declare class CSSPlugin implements Plugin {
10
10
  constructor(configManager: ConfigManager);
11
11
  getCompletions(document: AstroDocument, position: Position, completionContext?: CompletionContext): CompletionList | null;
12
12
  private getCompletionsInternal;
13
+ getDocumentColors(document: AstroDocument): ColorInformation[];
14
+ getColorPresentations(document: AstroDocument, range: Range, color: Color): ColorPresentation[];
13
15
  private inStyleAttributeWithoutInterpolation;
14
16
  /**
15
17
  * Get the associated CSS Document for a style tag
@@ -9,6 +9,7 @@ const language_service_1 = require("./language-service");
9
9
  const parseHtml_1 = require("../../core/documents/parseHtml");
10
10
  const StyleAttributeDocument_1 = require("./StyleAttributeDocument");
11
11
  const getIdClassCompletions_1 = require("./features/getIdClassCompletions");
12
+ const lodash_1 = require("lodash");
12
13
  class CSSPlugin {
13
14
  constructor(configManager) {
14
15
  this.__name = 'css';
@@ -67,6 +68,38 @@ class CSSPlugin {
67
68
  // Emmet completions change on every keystroke, so they are never complete
68
69
  emmetResults.items.length > 0);
69
70
  }
71
+ getDocumentColors(document) {
72
+ if (!this.featureEnabled('documentColors')) {
73
+ return [];
74
+ }
75
+ const allColorInfo = this.getCSSDocumentsForDocument(document).map((cssDoc) => {
76
+ const cssLang = extractLanguage(cssDoc);
77
+ const langService = (0, language_service_1.getLanguageService)(cssLang);
78
+ if (shouldExcludeColor(cssLang)) {
79
+ return [];
80
+ }
81
+ return langService
82
+ .findDocumentColors(cssDoc, cssDoc.stylesheet)
83
+ .map((colorInfo) => (0, documents_1.mapObjWithRangeToOriginal)(cssDoc, colorInfo));
84
+ });
85
+ return (0, lodash_1.flatten)(allColorInfo);
86
+ }
87
+ getColorPresentations(document, range, color) {
88
+ if (!this.featureEnabled('colorPresentations')) {
89
+ return [];
90
+ }
91
+ const allColorPres = this.getCSSDocumentsForDocument(document).map((cssDoc) => {
92
+ const cssLang = extractLanguage(cssDoc);
93
+ const langService = (0, language_service_1.getLanguageService)(cssLang);
94
+ if ((!cssDoc.isInGenerated(range.start) && !cssDoc.isInGenerated(range.end)) || shouldExcludeColor(cssLang)) {
95
+ return [];
96
+ }
97
+ return langService
98
+ .getColorPresentations(cssDoc, cssDoc.stylesheet, color, (0, documents_1.mapRangeToGenerated)(cssDoc, range))
99
+ .map((colorPres) => (0, documents_1.mapColorPresentationToOriginal)(cssDoc, colorPres));
100
+ });
101
+ return (0, lodash_1.flatten)(allColorPres);
102
+ }
70
103
  inStyleAttributeWithoutInterpolation(attrContext, text) {
71
104
  return (attrContext.name === 'style' &&
72
105
  !!attrContext.valueRange &&
@@ -102,6 +135,22 @@ class CSSPlugin {
102
135
  }
103
136
  }
104
137
  exports.CSSPlugin = CSSPlugin;
138
+ /**
139
+ * Exclude certain language when getting colors
140
+ * The CSS language service only supports CSS, LESS and SCSS,
141
+ * which mean that we cannot support colors in other languages
142
+ */
143
+ function shouldExcludeColor(document) {
144
+ const language = typeof document === 'string' ? document : extractLanguage(document);
145
+ switch (language) {
146
+ case 'sass':
147
+ case 'stylus':
148
+ case 'styl':
149
+ return true;
150
+ default:
151
+ return false;
152
+ }
153
+ }
105
154
  function isSASS(document) {
106
155
  switch (extractLanguage(document)) {
107
156
  case 'sass':
@@ -168,26 +168,21 @@ async function createLanguageService(tsconfigPath, docContext) {
168
168
  snapshotManager.updateNonAstroFile(fileName, changes);
169
169
  }
170
170
  function getParsedTSConfig() {
171
- var _a, _b, _c, _d;
171
+ var _a, _b, _c;
172
172
  let configJson = (tsconfigPath && typescript_1.default.readConfigFile(tsconfigPath, typescript_1.default.sys.readFile).config) || {};
173
173
  // If our user has types in their config but it doesn't include the types needed for Astro, add them to the config
174
174
  if (((_a = configJson.compilerOptions) === null || _a === void 0 ? void 0 : _a.types) && !((_b = configJson.compilerOptions) === null || _b === void 0 ? void 0 : _b.types.includes('astro/env'))) {
175
175
  configJson.compilerOptions.types.push('astro/env');
176
176
  }
177
177
  configJson.compilerOptions = Object.assign(getDefaultCompilerOptions(), configJson.compilerOptions);
178
- // If the user supplied exclude, let's use theirs
179
- (_c = configJson.exclude) !== null && _c !== void 0 ? _c : (configJson.exclude = getDefaultExclude());
180
178
  // Delete include so that .astro files don't get mistakenly excluded by the user
181
179
  delete configJson.include;
182
180
  // If the user supplied exclude, let's use theirs otherwise, use ours
183
- (_d = configJson.exclude) !== null && _d !== void 0 ? _d : (configJson.exclude = getDefaultExclude());
181
+ (_c = configJson.exclude) !== null && _c !== void 0 ? _c : (configJson.exclude = getDefaultExclude());
184
182
  // Everything here will always, unconditionally, be in the resulting config
185
183
  const forcedCompilerOptions = {
186
184
  // Our TSX is currently not typed, which unfortunately means that we can't support `noImplicitAny`
187
185
  noImplicitAny: false,
188
- // Most of the code people write in an .astro file is in the frontmatter which is executed server side
189
- // Thus, we don't want the DOM lib. We'll need to overwrite this for script tags however
190
- lib: ['ESNext'],
191
186
  noEmit: true,
192
187
  declaration: false,
193
188
  allowNonTsExtensions: true,
package/dist/server.js CHANGED
@@ -94,6 +94,7 @@ function startLanguageServer(connection) {
94
94
  ':',
95
95
  ],
96
96
  },
97
+ colorProvider: true,
97
98
  hoverProvider: true,
98
99
  signatureHelpProvider: {
99
100
  triggerCharacters: ['(', ',', '<'],
@@ -143,6 +144,8 @@ function startLanguageServer(connection) {
143
144
  }
144
145
  return pluginHost.resolveCompletion(data, completionItem);
145
146
  });
147
+ connection.onDocumentColor((params) => pluginHost.getDocumentColors(params.textDocument));
148
+ connection.onColorPresentation((params) => pluginHost.getColorPresentations(params.textDocument, params.range, params.color));
146
149
  connection.onRequest(TagCloseRequest, (evt) => pluginHost.doTagComplete(evt.textDocument, evt.position));
147
150
  connection.onSignatureHelp((evt, cancellationToken) => pluginHost.getSignatureHelp(evt.textDocument, evt.position, evt.context, cancellationToken));
148
151
  connection.onRenameRequest((evt) => pluginHost.rename(evt.textDocument, evt.position, evt.newName));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/language-server",
3
- "version": "0.12.0",
3
+ "version": "0.12.1",
4
4
  "author": "withastro",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",