Skip to content

Commit

Permalink
Merge pull request #93 from redhat-developer/configure-hover
Browse files Browse the repository at this point in the history
Added ability to toggle hover and autocompletion
  • Loading branch information
JPinkney committed Sep 27, 2018
2 parents 74f640a + ccad22f commit 47d45f7
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 17 deletions.
12 changes: 11 additions & 1 deletion src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { CompletionItem, CompletionItemKind, CompletionList, TextDocument, Posit

import * as nls from 'vscode-nls';
import { matchOffsetToDocument } from '../utils/arrUtils';
import { LanguageSettings } from '../yamlLanguageService';
const localize = nls.loadMessageBundle();


Expand All @@ -26,15 +27,20 @@ export class YAMLCompletion {
private contributions: JSONWorkerContribution[];
private promise: PromiseConstructor;
private customTags: Array<String>;
private completion: boolean;

constructor(schemaService: SchemaService.IJSONSchemaService, contributions: JSONWorkerContribution[] = [], promiseConstructor?: PromiseConstructor) {
this.schemaService = schemaService;
this.contributions = contributions;
this.promise = promiseConstructor || Promise;
this.customTags = [];
this.completion = true;
}

public configure(customTags: Array<String>){
public configure(languageSettings: LanguageSettings, customTags: Array<String>){
if (languageSettings) {
this.completion = languageSettings.completion;
}
this.customTags = customTags;
}

Expand All @@ -56,6 +62,10 @@ export class YAMLCompletion {
items: [],
isIncomplete: false
};

if (!this.completion) {
return Promise.resolve(result);
}

let offset = document.offsetAt(position);
if(document.getText()[offset] === ":"){
Expand Down
13 changes: 11 additions & 2 deletions src/languageservice/services/yamlHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,32 @@ import {PromiseConstructor, Thenable} from 'vscode-json-languageservice';

import {Hover, TextDocument, Position, Range, MarkedString} from 'vscode-languageserver-types';
import { matchOffsetToDocument } from '../utils/arrUtils';
import { LanguageSettings } from '../yamlLanguageService';

export class YAMLHover {

private schemaService: SchemaService.IJSONSchemaService;
private contributions: JSONWorkerContribution[];
private promise: PromiseConstructor;
private shouldHover: boolean;

constructor(schemaService: SchemaService.IJSONSchemaService, contributions: JSONWorkerContribution[] = [], promiseConstructor: PromiseConstructor) {
this.schemaService = schemaService;
this.contributions = contributions;
this.promise = promiseConstructor || Promise;
this.shouldHover = true;
}

public configure(languageSettings: LanguageSettings){
if(languageSettings){
this.shouldHover = languageSettings.hover;
}
}

public doHover(document: TextDocument, position: Position, doc): Thenable<Hover> {

if(!document){
this.promise.resolve(void 0);
if(!this.shouldHover || !document){
return this.promise.resolve(void 0);
}

let offset = document.offsetAt(position);
Expand Down
5 changes: 4 additions & 1 deletion src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { format } from './services/yamlFormatter';

export interface LanguageSettings {
validate?: boolean; //Setting for whether we want to validate the schema
hover?: boolean; //Setting for whether we want to have hover results
completion?: boolean; //Setting for whether we want to have completion results
isKubernetes?: boolean; //If true then its validating against kubernetes
schemas?: any[]; //List of schemas,
customTags?: Array<String>; //Array of Custom Tags
Expand Down Expand Up @@ -120,8 +122,9 @@ export function getLanguageService(schemaRequestService, workspaceContext, contr
});
}
yamlValidation.configure(settings);
hover.configure(settings);
let customTagsSetting = settings && settings["customTags"] ? settings["customTags"] : [];
completer.configure(customTagsSetting);
completer.configure(settings, customTagsSetting);
},
doComplete: completer.doComplete.bind(completer),
doResolve: completer.doResolve.bind(completer),
Expand Down
8 changes: 8 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ interface Settings {
format: { enable: boolean; };
schemas: JSONSchemaSettings[];
validate: boolean;
hover: boolean;
completion: boolean;
customTags: Array<String>;
};
http: {
Expand All @@ -196,6 +198,8 @@ let formatterRegistration: Thenable<Disposable> = null;
let specificValidatorPaths = [];
let schemaConfigurationSettings = [];
let yamlShouldValidate = true;
let yamlShouldHover = true;
let yamlShouldCompletion = true;
let schemaStoreSettings = [];
let customTags = [];

Expand All @@ -206,6 +210,8 @@ connection.onDidChangeConfiguration((change) => {
specificValidatorPaths = [];
yamlConfigurationSettings = settings.yaml && settings.yaml.schemas;
yamlShouldValidate = settings.yaml && settings.yaml.validate;
yamlShouldHover = settings.yaml && settings.yaml.hover;
yamlShouldCompletion = settings.yaml && settings.yaml.completion;
schemaConfigurationSettings = [];
customTags = settings.yaml && settings.yaml.customTags ? settings.yaml.customTags : [];

Expand Down Expand Up @@ -290,6 +296,8 @@ connection.onNotification(SchemaAssociationNotification.type, associations => {
function updateConfiguration() {
let languageSettings: LanguageSettings = {
validate: yamlShouldValidate,
hover: yamlShouldHover,
completion: yamlShouldCompletion,
schemas: [],
customTags: customTags
};
Expand Down
3 changes: 2 additions & 1 deletion test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ let schemaService = new JSONSchemaService(schemaRequestService, workspaceContext

let uri = 'http://json.schemastore.org/bowerrc';
let languageSettings = {
schemas: []
schemas: [],
completion: true
};
let fileMatch = ["*.yml", "*.yaml"];
languageSettings.schemas.push({ uri, fileMatch: fileMatch });
Expand Down
3 changes: 2 additions & 1 deletion test/autoCompletion2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ let schemaService = new JSONSchemaService(schemaRequestService, workspaceContext

let uri = 'http://json.schemastore.org/composer';
let languageSettings = {
schemas: []
schemas: [],
completion: true
};
let fileMatch = ["*.yml", "*.yaml"];
languageSettings.schemas.push({ uri, fileMatch: fileMatch });
Expand Down
3 changes: 2 additions & 1 deletion test/autoCompletion3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ let languageService = getLanguageService(schemaRequestService, workspaceContext,

let uri = 'http://json.schemastore.org/asmdef';
let languageSettings = {
schemas: []
schemas: [],
completion: true
};
let fileMatch = ["*.yml", "*.yaml"];
languageSettings.schemas.push({ uri, fileMatch: fileMatch });
Expand Down
7 changes: 4 additions & 3 deletions test/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
RequestType
} from 'vscode-languageserver';
import { xhr, XHRResponse, configure as configureHttpRequests, getErrorStatusDescription } from 'request-light';
import {getLanguageService} from '../src/languageservice/yamlLanguageService'
import {getLanguageService, LanguageSettings} from '../src/languageservice/yamlLanguageService'
import Strings = require( '../src/languageservice/utils/strings');
import URI from '../src/languageservice/utils/uri';
import * as URL from 'url';
Expand All @@ -26,8 +26,9 @@ let languageService = getLanguageService(schemaRequestService, workspaceContext,
let schemaService = new JSONSchemaService(schemaRequestService, workspaceContext);

let uri = 'http://json.schemastore.org/bowerrc';
let languageSettings = {
schemas: []
let languageSettings: LanguageSettings = {
schemas: [],
hover: true
};
let fileMatch = ["*.yml", "*.yaml"];
languageSettings.schemas.push({ uri, fileMatch: fileMatch });
Expand Down
7 changes: 4 additions & 3 deletions test/hover2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
RequestType
} from 'vscode-languageserver';
import { xhr, XHRResponse, configure as configureHttpRequests, getErrorStatusDescription } from 'request-light';
import {getLanguageService} from '../src/languageservice/yamlLanguageService'
import {getLanguageService, LanguageSettings} from '../src/languageservice/yamlLanguageService'
import Strings = require( '../src/languageservice/utils/strings');
import URI from '../src/languageservice/utils/uri';
import * as URL from 'url';
Expand All @@ -26,8 +26,9 @@ let languageService = getLanguageService(schemaRequestService, workspaceContext,
let schemaService = new JSONSchemaService(schemaRequestService, workspaceContext);

let uri = 'http://json.schemastore.org/composer';
let languageSettings = {
schemas: []
let languageSettings: LanguageSettings = {
schemas: [],
hover: true
};
let fileMatch = ["*.yml", "*.yaml"];
languageSettings.schemas.push({ uri, fileMatch: fileMatch });
Expand Down
48 changes: 48 additions & 0 deletions test/hover3.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { TextDocument } from 'vscode-languageserver';
import {getLanguageService, LanguageSettings} from '../src/languageservice/yamlLanguageService'
import {schemaRequestService, workspaceContext} from './testHelper';
import { parse as parseYAML } from '../src/languageservice/parser/yamlParser';
var assert = require('assert');

let languageService = getLanguageService(schemaRequestService, workspaceContext, [], null);

let uri = 'http://json.schemastore.org/bowerrc';
let languageSettings: LanguageSettings = {
schemas: [],
hover: false
};
let fileMatch = ["*.yml", "*.yaml"];
languageSettings.schemas.push({ uri, fileMatch: fileMatch });
languageService.configure(languageSettings);

suite("Hover Setting Tests", () => {

describe('Yaml Hover with bowerrc', function(){

describe('doComplete', function(){

function setup(content: string){
return TextDocument.create("file://~/Desktop/vscode-k8s/test.yaml", "yaml", 0, content);
}

function parseSetup(content: string, position){
let testTextDocument = setup(content);
let jsonDocument = parseYAML(testTextDocument.getText());
return languageService.doHover(testTextDocument, testTextDocument.positionAt(position), jsonDocument);
}

it('Hover should not return anything', (done) => {
let content = "cwd: test";
let hover = parseSetup(content, 1);
hover.then(function(result){
assert.equal(result, undefined);
}).then(done, done);
});

});
});
});
3 changes: 2 additions & 1 deletion test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ let schemaService = new JSONSchemaService(schemaRequestService, workspaceContext
let uri = "https://gist.githubusercontent.com/JPinkney/ccaf3909ef811e5657ca2e2e1fa05d76/raw/f85e51bfb67fdb99ab7653c2953b60087cc871ea/openshift_schema_all.json";
let languageSettings = {
schemas: [],
validate: true
validate: true,
completion: true
};
let fileMatch = ["*.yml", "*.yaml"];
languageSettings.schemas.push({ uri, fileMatch: fileMatch });
Expand Down
7 changes: 4 additions & 3 deletions test/mulipleDocuments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { TextDocument } from 'vscode-languageserver';
import {getLanguageService} from '../src/languageservice/yamlLanguageService'
import {getLanguageService, LanguageSettings} from '../src/languageservice/yamlLanguageService'
import path = require('path');
import {schemaRequestService, workspaceContext} from './testHelper';
import { parse as parseYAML } from '../src/languageservice/parser/yamlParser';
Expand All @@ -27,10 +27,11 @@ function toFsPath(str): string {
}

let uri = toFsPath(path.join(__dirname, './fixtures/customMultipleSchemaSequences.json'));
let languageSettings = {
let languageSettings: LanguageSettings = {
schemas: [],
validate: true,
customTags: []
customTags: [],
hover: true
};
let fileMatch = ["*.yml", "*.yaml"];
languageSettings.schemas.push({ uri, fileMatch: fileMatch });
Expand Down

0 comments on commit 47d45f7

Please sign in to comment.