Skip to content

Commit

Permalink
fix(windows): fix plugin not working with remote extn pack
Browse files Browse the repository at this point in the history
issue
- RelativePattern not working on `local os: windows`
  • Loading branch information
Mayank1791989 committed Aug 3, 2019
1 parent 22d8315 commit 7fd0579
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
13 changes: 8 additions & 5 deletions src/ClientStatusBarItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ export default class ClientStatusBarItem {
private _item: StatusBarItem;
private _client: LanguageClient;
private _disposables: Array<{ dispose: () => any }> = [];
private _canUseRelativePattern: boolean;

constructor(client: LanguageClient) {
constructor(client: LanguageClient, canUseRelativePattern: boolean) {
this._item = window.createStatusBarItem(StatusBarAlignment.Right, 0);
this._client = client;
this._canUseRelativePattern = canUseRelativePattern;

this._disposables.push(this._item);
this._disposables.push(this._addOnClickToShowOutputChannel());
Expand Down Expand Up @@ -113,16 +115,17 @@ export default class ClientStatusBarItem {
// @TODO: if possible, match against patterns defined in .gqlconfig
// instead of extensions.
const extensions = this._client.initializeResult.fileExtensions;
const pattern = `**/*.{${extensions.join(',')}}`;
const score = languages.match(
{
scheme: 'file',
pattern: new RelativePattern(
workspaceFolder,
`**/*.{${extensions.join(',')}}`,
),
pattern: this._canUseRelativePattern
? new RelativePattern(workspaceFolder, pattern)
: pattern,
},
textEditor.document,
);

hide = score === 0;
} else {
// while server is initializing show status bar item
Expand Down
26 changes: 19 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {

import { findConfigFile as findGQLConfigFile } from '@playlyfe/gql-language-server';
import ClientStatusBarItem from './ClientStatusBarItem';
import { isExtensionRunningLocally } from './utils';

const EXT_NAME = 'graphqlForVSCode';
const GQL_LANGUAGE_SERVER_CLI_PATH = require.resolve(
Expand All @@ -31,9 +32,11 @@ interface IClient {
const clients: Map<string, IClient | null> = new Map();

export function activate(context: ExtensionContext) {
createClientForWorkspaces();
createClientForWorkspaces(context);
// update clients when workspaceFolderChanges
Workspace.onDidChangeWorkspaceFolders(createClientForWorkspaces);
Workspace.onDidChangeWorkspaceFolders(() => {
createClientForWorkspaces(context);
});
}

export function deactivate(): Thenable<void> {
Expand All @@ -46,14 +49,14 @@ export function deactivate(): Thenable<void> {
return Promise.all(promises).then(() => undefined);
}

function createClientForWorkspaces() {
function createClientForWorkspaces(context: ExtensionContext) {
const workspaceFolders = Workspace.workspaceFolders || [];
const workspaceFoldersIndex: { [key: string]: boolean } = {};

workspaceFolders.forEach(folder => {
const key = folder.uri.toString();
if (!clients.has(key)) {
const client = createClientForWorkspace(folder);
const client = createClientForWorkspace(folder, context);
// console.log('adding client', key, client);
clients.set(key, client);
}
Expand All @@ -73,7 +76,10 @@ function createClientForWorkspaces() {
});
}

function createClientForWorkspace(folder: WorkspaceFolder): null | IClient {
function createClientForWorkspace(
folder: WorkspaceFolder,
context: ExtensionContext,
): null | IClient {
// per workspacefolder settings
const config = Workspace.getConfiguration(EXT_NAME, folder.uri);
const outputChannel = window.createOutputChannel(`GraphQL - ${folder.name}`);
Expand Down Expand Up @@ -121,6 +127,12 @@ function createClientForWorkspace(folder: WorkspaceFolder): null | IClient {
},
};

// TEMP_FIX: relativePattern is not working when extension is
// running using vscode-remote with `local os = windows`
// NOTE: relativePattern is used only for optimization so it will
// not change the extension behaviour
const canUseRelativePattern = isExtensionRunningLocally(context);

// Options to control the language client
const clientOptions: LanguageClientOptions = {
diagnosticCollectionName: 'graphql',
Expand All @@ -136,7 +148,7 @@ function createClientForWorkspace(folder: WorkspaceFolder): null | IClient {
outputChannel,
workspaceFolder: folder,
initializationOptions: {
relativePattern: true,
relativePattern: canUseRelativePattern,
},
};

Expand All @@ -148,7 +160,7 @@ function createClientForWorkspace(folder: WorkspaceFolder): null | IClient {
clientOptions,
);

const statusBarItem = new ClientStatusBarItem(client);
const statusBarItem = new ClientStatusBarItem(client, canUseRelativePattern);

const subscriptions = [
client.start(),
Expand Down
16 changes: 16 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ExtensionContext } from 'vscode';

// NOTE same as vscode.ExtensionExecutionContext
// this const can be replaced when we drop support of vscode <=1.35
const EXTENSION_EXECUTION_CONTEXT = {
Local: 1,
Remote: 2,
};

export function isExtensionRunningLocally(context: ExtensionContext): boolean {
// NOTE: executionContext is present in >=1.35 (when remote support added)
// so using local as default value
const executionContext =
(context as any).executionContext || EXTENSION_EXECUTION_CONTEXT.Local;
return executionContext === EXTENSION_EXECUTION_CONTEXT.Local;
}

0 comments on commit 7fd0579

Please sign in to comment.