Commit ee02432f authored by Olena Horal-Koretska's avatar Olena Horal-Koretska 2️⃣ Committed by Tristan Read
Browse files

feat: Add user-configured CS languages

parent 932cb5f6
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -65,6 +65,8 @@ To set up Code Suggestions:
   [meets all the requirements](https://docs.gitlab.com/ee/user/project/repository/code_suggestions/#use-code-suggestions)
   to use Code Suggestions.
1. [Set up this extension](#setup) and connect it to your instance.
1. Optional. To add more languages to Code Suggestions, see
   `gitlab.aiAssistedCodeSuggestions.languages` in [Extension settings](#extension-settings).

#### Use Code Generation

@@ -182,6 +184,10 @@ Defines the search queries that retrieves the items shown on the GitLab Panel. S

Toggle to enable or disable AI assisted code suggestions.

**`gitlab.aiAssistedCodeSuggestions.languages`**

(Experimental.) To expand the list of [officially supported languages](https://docs.gitlab.com/ee/user/project/repository/code_suggestions/supported_extensions.html#supported-languages) for Code Suggestions, provide a comma-separated list of the [language identifiers](https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers). This feature is experimental, and the code suggestions quality for the added languages might not be optimal.

## Features in-depth

### Issue and Merge Request details and comments in VS Code
+7 −7
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@
      "license": "MIT",
      "dependencies": {
        "@anycable/core": "^0.8.3",
        "@gitlab-org/gitlab-lsp": "^4.5.0",
        "@gitlab-org/gitlab-lsp": "^4.6.0",
        "@snowplow/tracker-core": "3.23.0",
        "cross-fetch": "^4.0.0",
        "dayjs": "^1.11.11",
@@ -1106,9 +1106,9 @@
      }
    },
    "node_modules/@gitlab-org/gitlab-lsp": {
      "version": "4.5.0",
      "resolved": "https://gitlab.com/api/v4/projects/46519181/packages/npm/@gitlab-org/gitlab-lsp/-/@gitlab-org/gitlab-lsp-4.5.0.tgz",
      "integrity": "sha1-LvOSH98Sca18FdWrhWqY7jeoq84=",
      "version": "4.6.0",
      "resolved": "https://gitlab.com/api/v4/projects/46519181/packages/npm/@gitlab-org/gitlab-lsp/-/@gitlab-org/gitlab-lsp-4.6.0.tgz",
      "integrity": "sha1-3qYQpfgwLFtY3lPKe/iyPoW5O9c=",
      "dependencies": {
        "@snowplow/tracker-core": "^3.23.0",
        "ajv": "^8.13.0",
@@ -14698,9 +14698,9 @@
      "dev": true
    },
    "@gitlab-org/gitlab-lsp": {
      "version": "4.5.0",
      "resolved": "https://gitlab.com/api/v4/projects/46519181/packages/npm/@gitlab-org/gitlab-lsp/-/@gitlab-org/gitlab-lsp-4.5.0.tgz",
      "integrity": "sha1-LvOSH98Sca18FdWrhWqY7jeoq84=",
      "version": "4.6.0",
      "resolved": "https://gitlab.com/api/v4/projects/46519181/packages/npm/@gitlab-org/gitlab-lsp/-/@gitlab-org/gitlab-lsp-4.6.0.tgz",
      "integrity": "sha1-3qYQpfgwLFtY3lPKe/iyPoW5O9c=",
      "requires": {
        "@snowplow/tracker-core": "^3.23.0",
        "ajv": "^8.13.0",
+6 −1
Original line number Diff line number Diff line
@@ -191,6 +191,11 @@
          "type": "string",
          "default": null
        },
        "gitlab.aiAssistedCodeSuggestions.languages": {
          "description": "Comma-separated list of additional languages to provide code suggestions for.",
          "type": "string",
          "default": null
        },
        "gitlab.duoChat.enabled": {
          "description": "Enable GitLab Duo Chat assistant",
          "type": "boolean",
@@ -301,7 +306,7 @@
  },
  "dependencies": {
    "@anycable/core": "^0.8.3",
    "@gitlab-org/gitlab-lsp": "^4.5.0",
    "@gitlab-org/gitlab-lsp": "^4.6.0",
    "@snowplow/tracker-core": "3.23.0",
    "cross-fetch": "^4.0.0",
    "dayjs": "^1.11.11",
+41 −0
Original line number Diff line number Diff line
@@ -3,6 +3,12 @@ import { createFakePartial } from '../../test_utils/create_fake_partial';
import { SupportedLanguagePolicy, UNSUPPORTED_LANGUAGE } from './supported_language_policy';
import { createActiveTextEditorChangeTrigger } from '../../test_utils/vscode_fakes';
import { AI_ASSISTED_CODE_SUGGESTIONS_LANGUAGES } from '../constants';
import {
  AiAssistedCodeSuggestionsConfiguration,
  getAiAssistedCodeSuggestionsConfiguration,
} from '../../utils/extension_configuration';

jest.mock('../../utils/extension_configuration');

describe('SupportedLanguagePolicy', () => {
  let policy: SupportedLanguagePolicy;
@@ -36,6 +42,41 @@ describe('SupportedLanguagePolicy', () => {
    });
  });

  describe('With user-extended list of supported languages', () => {
    let onDidChangeConfigurationListener: (e: vscode.ConfigurationChangeEvent) => any;

    const userLanguages = ['html', 'css', 'markdown'];

    beforeEach(() => {
      jest.mocked(vscode.workspace.onDidChangeConfiguration).mockImplementationOnce(listener => {
        onDidChangeConfigurationListener = listener;
        return {
          dispose: () => {},
        };
      });

      const aiCSConfig = createFakePartial<AiAssistedCodeSuggestionsConfiguration>({
        languages: userLanguages.join(','),
      });

      jest.mocked(getAiAssistedCodeSuggestionsConfiguration).mockReturnValue(aiCSConfig);

      policy = new SupportedLanguagePolicy();
    });

    it('is NOT engaged when language is not in the allow list, but was added as supported by the user ', () => {
      onDidChangeConfigurationListener({ affectsConfiguration: () => true });

      userLanguages.forEach(async languageId => {
        await triggerActiveTextEditorChange(
          createFakePartial<vscode.TextEditor>({ document: { languageId } }),
        );

        expect(policy.engaged).toBe(false);
      });
    });
  });

  it('fires `onEngagedChange` event when the engaged changes', async () => {
    const listener = jest.fn();
    policy.onEngagedChange(listener);
+26 −8
Original line number Diff line number Diff line
@@ -2,6 +2,10 @@ import * as vscode from 'vscode';
import { StatePolicy, VisibleState } from './state_policy';
import { diffEmitter } from '../diff_emitter';
import { AI_ASSISTED_CODE_SUGGESTIONS_LANGUAGES } from '../constants';
import {
  AI_ASSISTED_CODE_SUGGESTIONS_USER_LANGUAGES,
  getAiAssistedCodeSuggestionsConfiguration,
} from '../../utils/extension_configuration';

export const UNSUPPORTED_LANGUAGE: VisibleState = 'code-suggestions-document-unsupported-language';

@@ -12,19 +16,22 @@ export class SupportedLanguagePolicy implements StatePolicy {

  #isLanguageSupported = false;

  #codeSuggestionLanguages: string[] = [...AI_ASSISTED_CODE_SUGGESTIONS_LANGUAGES];

  constructor() {
    this.#setSupportedLanguages();
    this.#subscriptions.push(
      vscode.window.onDidChangeActiveTextEditor(async te => {
        this.#isLanguageSupported = te
          ? AI_ASSISTED_CODE_SUGGESTIONS_LANGUAGES.includes(te.document.languageId)
          : false;
        this.#eventEmitter.fire(this.engaged);
      vscode.workspace.onDidChangeConfiguration(e => {
        if (e.affectsConfiguration(AI_ASSISTED_CODE_SUGGESTIONS_USER_LANGUAGES)) {
          this.#setSupportedLanguages();
        }
      }),
      vscode.window.onDidChangeActiveTextEditor(te => this.#checkLanguageSupported(te)),
    );
  }

  async init() {
    await this.#checkLanguageSupported(vscode.window.activeTextEditor);
    this.#checkLanguageSupported(vscode.window.activeTextEditor);
  }

  get engaged() {
@@ -39,10 +46,21 @@ export class SupportedLanguagePolicy implements StatePolicy {
    this.#subscriptions.forEach(s => s.dispose());
  }

  async #checkLanguageSupported(textEditor?: vscode.TextEditor) {
  #checkLanguageSupported(textEditor?: vscode.TextEditor) {
    this.#isLanguageSupported = textEditor
      ? AI_ASSISTED_CODE_SUGGESTIONS_LANGUAGES.includes(textEditor.document.languageId)
      ? this.#codeSuggestionLanguages.includes(textEditor.document.languageId)
      : false;
    this.#eventEmitter.fire(this.engaged);
  }

  #setSupportedLanguages() {
    const userConfiguredLanguages =
      getAiAssistedCodeSuggestionsConfiguration()
        ?.languages?.split(',')
        .map(language => language.trim()) ?? [];
    this.#codeSuggestionLanguages = [
      ...AI_ASSISTED_CODE_SUGGESTIONS_LANGUAGES,
      ...userConfiguredLanguages,
    ];
  }
}
Loading