[PATCH] D65998: [clangd] Added the vscode SemanticHighlighting feature code but did not enable it in the client.

2019-08-12 Thread Johan Vikström via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
jvikstrom marked 5 inline comments as done.
Closed by commit rL368568: [clangd] Added the vscode SemanticHighlighting 
feature code but did not enable… (authored by jvikstrom, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D65998?vs=214613=214624#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D65998/new/

https://reviews.llvm.org/D65998

Files:
  clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json
  
clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
  
clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts

Index: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
===
--- clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
+++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
@@ -2,6 +2,95 @@
 import * as jsonc from "jsonc-parser";
 import * as path from 'path';
 import * as vscode from 'vscode';
+import * as vscodelc from 'vscode-languageclient';
+import * as vscodelct from 'vscode-languageserver-types';
+
+// Parameters for the semantic highlighting (server-side) push notification.
+// Mirrors the structure in the semantic highlighting proposal for LSP.
+interface SemanticHighlightingParams {
+  // The text document that has to be decorated with the semantic highlighting
+  // information.
+  textDocument: vscodelct.VersionedTextDocumentIdentifier;
+  // An array of semantic highlighting information.
+  lines: SemanticHighlightingInformation[];
+}
+// Contains the highlighting information for a specified line. Mirrors the
+// structure in the semantic highlighting proposal for LSP.
+interface SemanticHighlightingInformation {
+  // The zero-based line position in the text document.
+  line: number;
+  // A base64 encoded string representing every single highlighted characters
+  // with its start position, length and the "lookup table" index of of the
+  // semantic highlighting Text Mate scopes.
+  tokens?: string;
+}
+
+// A SemanticHighlightingToken decoded from the base64 data sent by clangd.
+interface SemanticHighlightingToken {
+  // Start column for this token.
+  character: number;
+  // Length of the token.
+  length: number;
+  // The TextMate scope index to the clangd scope lookup table.
+  scopeIndex: number;
+}
+
+// Language server push notification providing the semantic highlighting
+// information for a text document.
+export const NotificationType =
+new vscodelc.NotificationType(
+'textDocument/semanticHighlighting');
+
+// The feature that should be registered in the vscode lsp for enabling
+// experimental semantic highlighting.
+export class SemanticHighlightingFeature implements vscodelc.StaticFeature {
+  // The TextMate scope lookup table. A token with scope index i has the scopes
+  // on index i in the lookup table.
+  scopeLookupTable: string[][];
+  fillClientCapabilities(capabilities: vscodelc.ClientCapabilities) {
+// Extend the ClientCapabilities type and add semantic highlighting
+// capability to the object.
+const textDocumentCapabilities: vscodelc.TextDocumentClientCapabilities&
+{semanticHighlightingCapabilities?: {semanticHighlighting : boolean}} =
+capabilities.textDocument;
+textDocumentCapabilities.semanticHighlightingCapabilities = {
+  semanticHighlighting : true,
+};
+  }
+
+  initialize(capabilities: vscodelc.ServerCapabilities,
+ documentSelector: vscodelc.DocumentSelector|undefined) {
+// The semantic highlighting capability information is in the capabilities
+// object but to access the data we must first extend the ServerCapabilities
+// type.
+const serverCapabilities: vscodelc.ServerCapabilities&
+{semanticHighlighting?: {scopes : string[][]}} = capabilities;
+if (!serverCapabilities.semanticHighlighting)
+  return;
+this.scopeLookupTable = serverCapabilities.semanticHighlighting.scopes;
+  }
+
+  handleNotification(params: SemanticHighlightingParams) {}
+}
+
+// Converts a string of base64 encoded tokens into the corresponding array of
+// HighlightingTokens.
+export function decodeTokens(tokens: string): SemanticHighlightingToken[] {
+  const scopeMask = 0x;
+  const lenShift = 0x10;
+  const uint32Size = 4;
+  const buf = Buffer.from(tokens, 'base64');
+  const retTokens = [];
+  for (let i = 0, end = buf.length / uint32Size; i < end; i += 2) {
+const start = buf.readUInt32BE(i * uint32Size);
+const lenKind = buf.readUInt32BE((i + 1) * uint32Size);
+const scopeIndex = lenKind & scopeMask;
+const len = lenKind >>> lenShift;
+retTokens.push({character : start, scopeIndex : scopeIndex, length : len});

[PATCH] D65998: [clangd] Added the vscode SemanticHighlighting feature code but did not enable it in the client.

2019-08-12 Thread Haojian Wu via Phabricator via cfe-commits
hokein accepted this revision.
hokein added a comment.
This revision is now accepted and ready to land.

mostly good with a few nits.




Comment at: 
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts:9
+// Parameters for the semantic highlighting (server-side) push notification.
+interface SemanticHighlightingParams {
+  // The text document that has to be decorated with the semantic highlighting

could you add a comment describing this is a mirror of LSP definition?




Comment at: 
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts:16
+}
+
+// Contains the highlighting information for a specified line.

nit: I'd remove this blank line to make `SemanticHighlightingParams` and 
`SemanticHighlightingInformation` group closer as they are mirrors of LSP 
definitions.



Comment at: 
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts:27
+
+// A single SemanticHighlightingToken that clangd sent.
+interface SemanticHighlightingToken {

the comment seems not precise to me, this is the data decoded from the 
base64-encoded string sent by clangd.



Comment at: 
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts:33
+  length: number;
+  // The TextMate scope index to the clangd scopes.
+  scope: number;

nit: scope lookup table.



Comment at: 
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts:34
+  // The TextMate scope index to the clangd scopes.
+  scope: number;
+}

nit: maybe name it `scopeIndex`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D65998/new/

https://reviews.llvm.org/D65998



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D65998: [clangd] Added the vscode SemanticHighlighting feature code but did not enable it in the client.

2019-08-12 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 214613.
jvikstrom marked 4 inline comments as done.
jvikstrom added a comment.

Mirror the LSP proposal SemanticHighlightingParams/Information types.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D65998/new/

https://reviews.llvm.org/D65998

Files:
  clang-tools-extra/clangd/clients/clangd-vscode/package.json
  clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
  
clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts

Index: clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts
===
--- clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts
+++ clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts
@@ -3,7 +3,7 @@
 
 import * as TM from '../src/semantic-highlighting';
 
-suite('TextMate Tests', () => {
+suite('SemanticHighlighting Tests', () => {
   test('Parses arrays of textmate themes.', async () => {
 const themePath =
 path.join(__dirname, '../../test/assets/includeTheme.jsonc');
@@ -15,4 +15,24 @@
 assert.deepEqual(getScopeRule('b'), {scope : 'b', textColor : '#000'});
 assert.deepEqual(getScopeRule('c'), {scope : 'c', textColor : '#bcd'});
   });
+  test('Decodes tokens correctly', () => {
+const testCases: string[] = [
+  'AAABAAA=', 'AAADAAkEAAEAAA==',
+  'AAADAAkEAAEAAAoAAQAA'
+];
+const expected = [
+  [ {character : 0, scope : 0, length : 1} ],
+  [
+{character : 0, scope : 9, length : 3},
+{character : 4, scope : 0, length : 1}
+  ],
+  [
+{character : 0, scope : 9, length : 3},
+{character : 4, scope : 0, length : 1},
+{character : 10, scope : 0, length : 1}
+  ]
+];
+testCases.forEach((testCase, i) => assert.deepEqual(
+  TM.decodeTokens(testCase), expected[i]));
+  });
 });
Index: clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
===
--- clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
+++ clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
@@ -2,6 +2,94 @@
 import * as jsonc from "jsonc-parser";
 import * as path from 'path';
 import * as vscode from 'vscode';
+import * as vscodelc from 'vscode-languageclient';
+import * as vscodelct from 'vscode-languageserver-types';
+
+// Parameters for the semantic highlighting (server-side) push notification.
+interface SemanticHighlightingParams {
+  // The text document that has to be decorated with the semantic highlighting
+  // information.
+  textDocument: vscodelct.VersionedTextDocumentIdentifier;
+  // An array of semantic highlighting information.
+  lines: SemanticHighlightingInformation[];
+}
+
+// Contains the highlighting information for a specified line.
+interface SemanticHighlightingInformation {
+  // The zero-based line position in the text document.
+  line: number;
+  // A base64 encoded string representing every single highlighted characters
+  // with its start position, length and the "lookup table" index of of the
+  // semantic highlighting Text Mate scopes.
+  tokens?: string;
+}
+
+// A single SemanticHighlightingToken that clangd sent.
+interface SemanticHighlightingToken {
+  // Start column for this token.
+  character: number;
+  // Length of the token.
+  length: number;
+  // The TextMate scope index to the clangd scopes.
+  scope: number;
+}
+
+// Language server push notification providing the semantic highlighting
+// information for a text document.
+export const NotificationType =
+new vscodelc.NotificationType(
+'textDocument/semanticHighlighting');
+
+// The feature that should be registered in the vscode lsp for enabling
+// experimental semantic highlighting.
+export class SemanticHighlightingFeature implements vscodelc.StaticFeature {
+  // The TextMate scope lookup table. A token with scope index i has the scopes
+  // on index i in the lookup table.
+  scopeLookupTable: string[][];
+  fillClientCapabilities(capabilities: vscodelc.ClientCapabilities) {
+// Extend the ClientCapabilities type and add semantic highlighting
+// capability to the object.
+const textDocumentCapabilities: vscodelc.TextDocumentClientCapabilities&
+{semanticHighlightingCapabilities?: {semanticHighlighting : boolean}} =
+capabilities.textDocument;
+textDocumentCapabilities.semanticHighlightingCapabilities = {
+  semanticHighlighting : true,
+};
+  }
+
+  initialize(capabilities: vscodelc.ServerCapabilities,
+ documentSelector: vscodelc.DocumentSelector|undefined) {
+// The semantic highlighting capability information is in the capabilities
+// object but to access the data we must first 

[PATCH] D65998: [clangd] Added the vscode SemanticHighlighting feature code but did not enable it in the client.

2019-08-12 Thread Haojian Wu via Phabricator via cfe-commits
hokein added inline comments.



Comment at: 
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts:8
+// The information clangd sends when highlightings should be updated.
+interface SemanticHighlightingParams {
+  // The information about the text document where these highlightings should 
be

sorry for not being clear in the previous comment, I think we should mirror 
definitions from the LSP 
[proposal](https://github.com/microsoft/vscode-languageserver-node/pull/367/files#diff-ac2186ae4b8ba5cc9f17deebc42c9e28R13),
 the same to the `SemanticHighlightingInformation`.

You may need to add a `vscode-languageserver-types` dependency.



Comment at: 
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts:32
+
+export const NotificationType = new vscodelc.NotificationType<{}, void>(
+'textDocument/semanticHighlighting');

I think here we should use `SemanticHighlightingParams` in the generic type 
parameter list



Comment at: 
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts:38
+export class SemanticHighlightingFeature implements vscodelc.StaticFeature {
+  scopeLookupTable: string[][];
+  fillClientCapabilities(capabilities: vscodelc.ClientCapabilities) {

could you add a comment on this?



Comment at: 
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts:63
+  handleNotification(params: SemanticHighlightingParams) {
+const tokenLines =
+params.lines.map((line): SemanticHighlightingInformation => {

this seems doesn't do anything, can we defer it to a follow-up patch?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D65998/new/

https://reviews.llvm.org/D65998



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D65998: [clangd] Added the vscode SemanticHighlighting feature code but did not enable it in the client.

2019-08-09 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 214364.
jvikstrom marked 4 inline comments as done.
jvikstrom added a comment.

Address comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D65998/new/

https://reviews.llvm.org/D65998

Files:
  clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
  
clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts

Index: clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts
===
--- clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts
+++ clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts
@@ -3,7 +3,7 @@
 
 import * as TM from '../src/semantic-highlighting';
 
-suite('TextMate Tests', () => {
+suite('SemanticHighlighting Tests', () => {
   test('Parses arrays of textmate themes.', async () => {
 const themePath =
 path.join(__dirname, '../../test/assets/includeTheme.jsonc');
@@ -15,4 +15,24 @@
 assert.deepEqual(getScopeRule('b'), {scope : 'b', textColor : '#000'});
 assert.deepEqual(getScopeRule('c'), {scope : 'c', textColor : '#bcd'});
   });
+  test('Decodes tokens correctly', () => {
+const testCases: string[] = [
+  'AAABAAA=', 'AAADAAkEAAEAAA==',
+  'AAADAAkEAAEAAAoAAQAA'
+];
+const expected = [
+  [ {character : 0, scope : 0, length : 1} ],
+  [
+{character : 0, scope : 9, length : 3},
+{character : 4, scope : 0, length : 1}
+  ],
+  [
+{character : 0, scope : 9, length : 3},
+{character : 4, scope : 0, length : 1},
+{character : 10, scope : 0, length : 1}
+  ]
+];
+testCases.forEach((testCase, i) => assert.deepEqual(
+  TM.decodeTokens(testCase), expected[i]));
+  });
 });
Index: clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
===
--- clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
+++ clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
@@ -2,6 +2,89 @@
 import * as jsonc from "jsonc-parser";
 import * as path from 'path';
 import * as vscode from 'vscode';
+import * as vscodelc from 'vscode-languageclient';
+
+// The information clangd sends when highlightings should be updated.
+interface SemanticHighlightingParams {
+  // The information about the text document where these highlightings should be
+  // applied.
+  textDocument: {uri: string;};
+  // All the lines of highlightings that should be applied.
+  lines: [ {
+line: number;
+tokens: string;
+  } ];
+}
+// A single SemanticHighlightingToken that clangd sent.
+interface SemanticHighlightingToken {
+  // Start column for this token.
+  character: number;
+  // The TextMate scope index to the clangd scopes.
+  scope: number;
+  length: number;
+}
+// All highlightings for line.
+interface SemanticHighlightingInformation {
+  line: number;
+  tokens: SemanticHighlightingToken[];
+}
+
+export const NotificationType = new vscodelc.NotificationType<{}, void>(
+'textDocument/semanticHighlighting');
+
+// The feature that should be registered in the vscode lsp for enabling
+// experimental semantic highlighting.
+export class SemanticHighlightingFeature implements vscodelc.StaticFeature {
+  scopeLookupTable: string[][];
+  fillClientCapabilities(capabilities: vscodelc.ClientCapabilities) {
+// Extend the ClientCapabilities type and add semantic highlighting
+// capability to the object.
+const textDocumentCapabilities: vscodelc.TextDocumentClientCapabilities&
+{semanticHighlightingCapabilities?: {semanticHighlighting : boolean}} =
+capabilities.textDocument;
+textDocumentCapabilities.semanticHighlightingCapabilities = {
+  semanticHighlighting : true,
+};
+  }
+
+  initialize(capabilities: vscodelc.ServerCapabilities,
+ documentSelector: vscodelc.DocumentSelector|undefined) {
+// The semantic highlighting capability information is in the capabilities
+// object but to access the data we must first extend the ServerCapabilities
+// type.
+const serverCapabilities: vscodelc.ServerCapabilities&
+{semanticHighlighting?: {scopes : string[][]}} = capabilities;
+if (!serverCapabilities.semanticHighlighting)
+  return;
+this.scopeLookupTable = serverCapabilities.semanticHighlighting.scopes;
+  }
+
+  handleNotification(params: SemanticHighlightingParams) {
+const tokenLines =
+params.lines.map((line): SemanticHighlightingInformation => {
+  return { line: line.line, tokens: decodeTokens(line.tokens), }
+});
+  }
+}
+
+// Converts a string of base64 encoded tokens into the corresponding array of
+// HighlightingTokens.
+export function decodeTokens(tokens: 

[PATCH] D65998: [clangd] Added the vscode SemanticHighlighting feature code but did not enable it in the client.

2019-08-09 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom added inline comments.



Comment at: 
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts:62
+
+  handleNotification(params: HighlightingInformation) {
+const tokenLines = params.lines.map((line): HighlightingLine => {

hokein wrote:
> I think the params here is defined as `SemanticHighlightingParams` from the 
> LSP proposal, could we use the names as defined in the LSP for these 
> structures (e.g. `SemanticHighlightingParams`, 
> `SemanticHighlightingInformation`)?
Renaming HighlightingToken to SemanticHighlightingToken to be consistent with 
those names as well.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D65998/new/

https://reviews.llvm.org/D65998



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D65998: [clangd] Added the vscode SemanticHighlighting feature code but did not enable it in the client.

2019-08-09 Thread Haojian Wu via Phabricator via cfe-commits
hokein added inline comments.



Comment at: 
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts:37
+// experimental semantic highlighting.
+export class Feature implements vscodelc.StaticFeature {
+  scopes: string[];

nit: name it `SemanticHighlightingFeature`.



Comment at: 
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts:38
+export class Feature implements vscodelc.StaticFeature {
+  scopes: string[];
+  fillClientCapabilities(capabilities: vscodelc.ClientCapabilities) {

I believe the type of lookup table returned from clangd is a string[][]. I'd 
name it `scopeLookupTable`



Comment at: 
clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts:62
+
+  handleNotification(params: HighlightingInformation) {
+const tokenLines = params.lines.map((line): HighlightingLine => {

I think the params here is defined as `SemanticHighlightingParams` from the LSP 
proposal, could we use the names as defined in the LSP for these structures 
(e.g. `SemanticHighlightingParams`, `SemanticHighlightingInformation`)?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D65998/new/

https://reviews.llvm.org/D65998



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D65998: [clangd] Added the vscode SemanticHighlighting feature code but did not enable it in the client.

2019-08-09 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom updated this revision to Diff 214334.
jvikstrom added a comment.

Added comment to decodeTokens function.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D65998/new/

https://reviews.llvm.org/D65998

Files:
  clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
  
clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts

Index: clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts
===
--- clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts
+++ clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts
@@ -3,7 +3,7 @@
 
 import * as TM from '../src/semantic-highlighting';
 
-suite('TextMate Tests', () => {
+suite('SemanticHighlighting Tests', () => {
   test('Parses arrays of textmate themes.', async () => {
 const themePath =
 path.join(__dirname, '../../test/assets/includeTheme.jsonc');
@@ -15,4 +15,24 @@
 assert.deepEqual(getScopeRule('b'), {scope : 'b', textColor : '#000'});
 assert.deepEqual(getScopeRule('c'), {scope : 'c', textColor : '#bcd'});
   });
+  test('Decodes tokens correctly', () => {
+const testCases: string[] = [
+  'AAABAAA=', 'AAADAAkEAAEAAA==',
+  'AAADAAkEAAEAAAoAAQAA'
+];
+const expected = [
+  [ {character : 0, scope : 0, length : 1} ],
+  [
+{character : 0, scope : 9, length : 3},
+{character : 4, scope : 0, length : 1}
+  ],
+  [
+{character : 0, scope : 9, length : 3},
+{character : 4, scope : 0, length : 1},
+{character : 10, scope : 0, length : 1}
+  ]
+];
+testCases.forEach((testCase, i) => assert.deepEqual(
+  TM.decodeTokens(testCase), expected[i]));
+  });
 });
Index: clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
===
--- clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
+++ clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
@@ -2,6 +2,88 @@
 import * as jsonc from "jsonc-parser";
 import * as path from 'path';
 import * as vscode from 'vscode';
+import * as vscodelc from 'vscode-languageclient';
+
+// The information clangd sends when highlightings should be updated.
+interface HighlightingInformation {
+  // The information about the text document where these highlightings should be
+  // applied.
+  textDocument: {uri: string;};
+  // All the lines of highlightings that should be applied.
+  lines: [ {
+line: number;
+tokens: string;
+  } ];
+}
+// A single HighlightingToken that clangd sent.
+interface HighlightingToken {
+  // Start column for this token.
+  character: number;
+  // The TextMate scope index to the clangd scopes.
+  scope: number;
+  length: number;
+}
+// All highlightings for line.
+interface HighlightingLine {
+  line: number;
+  tokens: HighlightingToken[];
+}
+
+export const NotificationType = new vscodelc.NotificationType<{}, void>(
+'textDocument/semanticHighlighting');
+
+// The feature that should be registered in the vscode lsp for enabling
+// experimental semantic highlighting.
+export class Feature implements vscodelc.StaticFeature {
+  scopes: string[];
+  fillClientCapabilities(capabilities: vscodelc.ClientCapabilities) {
+// Extend the ClientCapabilities type and add semantic highlighting
+// capability to the object.
+const textDocumentCapabilities: vscodelc.TextDocumentClientCapabilities&
+{semanticHighlightingCapabilities?: {semanticHighlighting : boolean}} =
+capabilities.textDocument;
+textDocumentCapabilities.semanticHighlightingCapabilities = {
+  semanticHighlighting : true,
+};
+  }
+
+  initialize(capabilities: vscodelc.ServerCapabilities,
+ documentSelector: vscodelc.DocumentSelector|undefined) {
+// The semantic highlighting capability information is in the capabilities
+// object but to access the data we must first extend the ServerCapabilities
+// type.
+const serverCapabilities: vscodelc.ServerCapabilities&
+{semanticHighlighting?: {scopes : string[]}} = capabilities;
+if (!serverCapabilities.semanticHighlighting)
+  return;
+this.scopes = serverCapabilities.semanticHighlighting.scopes;
+  }
+
+  handleNotification(params: HighlightingInformation) {
+const tokenLines = params.lines.map((line): HighlightingLine => {
+  return { line: line.line, tokens: decodeTokens(line.tokens), }
+});
+  }
+}
+
+// Converts a string of base64 encoded tokens into the corresponding array of
+// HighlightingTokens.
+export function decodeTokens(tokens: string): HighlightingToken[] {
+  const scopeMask = 0x;
+  const lenShift = 0x10;
+  const uint32Size = 4;
+  const buf = 

[PATCH] D65998: [clangd] Added the vscode SemanticHighlighting feature code but did not enable it in the client.

2019-08-09 Thread Johan Vikström via Phabricator via cfe-commits
jvikstrom created this revision.
jvikstrom added reviewers: hokein, ilya-biryukov.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay.
Herald added a project: clang.
jvikstrom updated this revision to Diff 214334.
jvikstrom added a comment.

Added comment to decodeTokens function.


Added the code for the StaticFeature that must be registered to the client. 
Also decoding the notification data into objects. Did not register it to the 
client yet.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D65998

Files:
  clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
  
clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts

Index: clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts
===
--- clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts
+++ clang-tools-extra/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts
@@ -3,7 +3,7 @@
 
 import * as TM from '../src/semantic-highlighting';
 
-suite('TextMate Tests', () => {
+suite('SemanticHighlighting Tests', () => {
   test('Parses arrays of textmate themes.', async () => {
 const themePath =
 path.join(__dirname, '../../test/assets/includeTheme.jsonc');
@@ -15,4 +15,24 @@
 assert.deepEqual(getScopeRule('b'), {scope : 'b', textColor : '#000'});
 assert.deepEqual(getScopeRule('c'), {scope : 'c', textColor : '#bcd'});
   });
+  test('Decodes tokens correctly', () => {
+const testCases: string[] = [
+  'AAABAAA=', 'AAADAAkEAAEAAA==',
+  'AAADAAkEAAEAAAoAAQAA'
+];
+const expected = [
+  [ {character : 0, scope : 0, length : 1} ],
+  [
+{character : 0, scope : 9, length : 3},
+{character : 4, scope : 0, length : 1}
+  ],
+  [
+{character : 0, scope : 9, length : 3},
+{character : 4, scope : 0, length : 1},
+{character : 10, scope : 0, length : 1}
+  ]
+];
+testCases.forEach((testCase, i) => assert.deepEqual(
+  TM.decodeTokens(testCase), expected[i]));
+  });
 });
Index: clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
===
--- clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
+++ clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts
@@ -2,6 +2,88 @@
 import * as jsonc from "jsonc-parser";
 import * as path from 'path';
 import * as vscode from 'vscode';
+import * as vscodelc from 'vscode-languageclient';
+
+// The information clangd sends when highlightings should be updated.
+interface HighlightingInformation {
+  // The information about the text document where these highlightings should be
+  // applied.
+  textDocument: {uri: string;};
+  // All the lines of highlightings that should be applied.
+  lines: [ {
+line: number;
+tokens: string;
+  } ];
+}
+// A single HighlightingToken that clangd sent.
+interface HighlightingToken {
+  // Start column for this token.
+  character: number;
+  // The TextMate scope index to the clangd scopes.
+  scope: number;
+  length: number;
+}
+// All highlightings for line.
+interface HighlightingLine {
+  line: number;
+  tokens: HighlightingToken[];
+}
+
+export const NotificationType = new vscodelc.NotificationType<{}, void>(
+'textDocument/semanticHighlighting');
+
+// The feature that should be registered in the vscode lsp for enabling
+// experimental semantic highlighting.
+export class Feature implements vscodelc.StaticFeature {
+  scopes: string[];
+  fillClientCapabilities(capabilities: vscodelc.ClientCapabilities) {
+// Extend the ClientCapabilities type and add semantic highlighting
+// capability to the object.
+const textDocumentCapabilities: vscodelc.TextDocumentClientCapabilities&
+{semanticHighlightingCapabilities?: {semanticHighlighting : boolean}} =
+capabilities.textDocument;
+textDocumentCapabilities.semanticHighlightingCapabilities = {
+  semanticHighlighting : true,
+};
+  }
+
+  initialize(capabilities: vscodelc.ServerCapabilities,
+ documentSelector: vscodelc.DocumentSelector|undefined) {
+// The semantic highlighting capability information is in the capabilities
+// object but to access the data we must first extend the ServerCapabilities
+// type.
+const serverCapabilities: vscodelc.ServerCapabilities&
+{semanticHighlighting?: {scopes : string[]}} = capabilities;
+if (!serverCapabilities.semanticHighlighting)
+  return;
+this.scopes = serverCapabilities.semanticHighlighting.scopes;
+  }
+
+  handleNotification(params: HighlightingInformation) {
+const tokenLines = params.lines.map((line): HighlightingLine => {
+  return { line: line.line, tokens: decodeTokens(line.tokens), }
+});