Skip to content

Commit

Permalink
Merge branch 'main' into feat/add-title-source-hover-settings
Browse files Browse the repository at this point in the history
  • Loading branch information
q-rault authored Jun 21, 2023
2 parents a37b053 + b0f60d4 commit 7a66521
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 11 deletions.
26 changes: 17 additions & 9 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ on:
pull_request:
branches: [main]

permissions:
contents: read


# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
permissions:
checks: write ## for coveralls
contents: read ## for docker-push
security-events: write ## for upload-sarif
# The type of runner that the job will run on
runs-on: ${{ matrix.os }}
strategy:
Expand All @@ -23,11 +31,11 @@ jobs:
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

# Set up Node
- name: Use Node 16
uses: actions/setup-node@v1
uses: actions/setup-node@7c12f8017d5436eb855f1ed4399f037a36fbd9e8 # v2.5.2
with:
node-version: 16
registry-url: "https://registry.npmjs.org"
Expand Down Expand Up @@ -56,13 +64,13 @@ jobs:

# Run tests
- name: Run Test
uses: GabrielBB/xvfb-action@fe2609f8182a9ed5aee7d53ff3ed04098a904df2 #v1.0
uses: coactions/setup-xvfb@b6b4fcfb9f5a895edadc3bc76318fae0ac17c8b3 # v1.0.1
with:
run: yarn coveralls

# Run Coveralls
- name: Coveralls
uses: coverallsapp/github-action@3284643be2c47fb6432518ecec17f1255e8a06a6 #master
uses: coverallsapp/github-action@c7885c00cb7ec0b8f9f5ff3f53cddb980f7a4412 # v2.2.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

Expand All @@ -75,17 +83,17 @@ jobs:
# Setup QEMU as requirement for docker
- name: Set up QEMU
if: ${{ success() && runner.os == 'Linux' && github.event_name == 'push' && github.ref == 'refs/heads/main'}}
uses: docker/setup-qemu-action@v1

uses: docker/setup-qemu-action@2b82ce82d56a2a04d2637cd93a637ae1b359c0a7 # v2.2.0
# Setup DockerBuildx as requirement for docker
- name: Set up Docker Buildx
if: ${{ success() && runner.os == 'Linux' && github.event_name == 'push' && github.ref == 'refs/heads/main'}}
uses: docker/setup-buildx-action@v1
uses: docker/setup-buildx-action@6a58db7e0d21ca03e6c44877909e80e45217eed2 # v2.6.0

# Login to Quay
- name: Login to Quay
if: ${{ success() && runner.os == 'Linux' && github.event_name == 'push' && github.ref == 'refs/heads/main'}}
uses: docker/login-action@v1
uses: docker/login-action@465a07811f14bebb1938fbed4728c6a1ff8901fc # v2.2.0
with:
registry: quay.io
username: ${{ secrets.QUAY_USERNAME }}
Expand All @@ -94,7 +102,7 @@ jobs:
# Build and push the latest version of yaml language server image
- name: Build and push
if: ${{ success() && runner.os == 'Linux' && github.event_name == 'push' && github.ref == 'refs/heads/main'}}
uses: docker/build-push-action@v2
uses: docker/build-push-action@44ea916f6c540f9302d50c2b1e5a8dc071f15cdf #v4.1.0
with:
context: .
file: ./Dockerfile
Expand Down
15 changes: 14 additions & 1 deletion src/languageserver/handlers/languageHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { FoldingRange } from 'vscode-json-languageservice';

import { Connection } from 'vscode-languageserver';
import {
CodeActionParams,
Expand All @@ -12,6 +12,7 @@ import {
DocumentOnTypeFormattingParams,
DocumentSymbolParams,
FoldingRangeParams,
SelectionRangeParams,
TextDocumentPositionParams,
CodeLensParams,
DefinitionParams,
Expand All @@ -24,6 +25,8 @@ import {
DocumentLink,
DocumentSymbol,
Hover,
FoldingRange,
SelectionRange,
SymbolInformation,
TextEdit,
} from 'vscode-languageserver-types';
Expand Down Expand Up @@ -61,6 +64,7 @@ export class LanguageHandlers {
this.connection.onCompletion((textDocumentPosition) => this.completionHandler(textDocumentPosition));
this.connection.onDidChangeWatchedFiles((change) => this.watchedFilesHandler(change));
this.connection.onFoldingRanges((params) => this.foldingRangeHandler(params));
this.connection.onSelectionRanges((params) => this.selectionRangeHandler(params));
this.connection.onCodeAction((params) => this.codeActionHandler(params));
this.connection.onDocumentOnTypeFormatting((params) => this.formatOnTypeHandler(params));
this.connection.onCodeLens((params) => this.codeLensHandler(params));
Expand Down Expand Up @@ -207,6 +211,15 @@ export class LanguageHandlers {
return this.languageService.getFoldingRanges(textDocument, context);
}

selectionRangeHandler(params: SelectionRangeParams): SelectionRange[] | undefined {
const textDocument = this.yamlSettings.documents.get(params.textDocument.uri);
if (!textDocument) {
return;
}

return this.languageService.getSelectionRanges(textDocument, params.positions);
}

codeActionHandler(params: CodeActionParams): CodeAction[] | undefined {
const textDocument = this.yamlSettings.documents.get(params.textDocument.uri);
if (!textDocument) {
Expand Down
2 changes: 1 addition & 1 deletion src/languageservice/services/yamlHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class YAMLHover {

const createHover = (contents: string): Hover => {
if (this.indentation !== undefined) {
const indentationMatchRegex = new RegExp(this.indentation, 'g');
const indentationMatchRegex = new RegExp(` {${this.indentation.length}}`, 'g');
contents = contents.replace(indentationMatchRegex, ' ');
}

Expand Down
107 changes: 107 additions & 0 deletions src/languageservice/services/yamlSelectionRanges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { Position, Range, SelectionRange } from 'vscode-languageserver-types';
import { yamlDocumentsCache } from '../parser/yaml-documents';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { ASTNode } from 'vscode-json-languageservice';

export function getSelectionRanges(document: TextDocument, positions: Position[]): SelectionRange[] | undefined {
if (!document) {
return;
}
const doc = yamlDocumentsCache.getYamlDocument(document);
return positions.map((position) => {
const ranges = getRanges(position);
let current: SelectionRange;
for (const range of ranges) {
current = SelectionRange.create(range, current);
}
if (!current) {
current = SelectionRange.create({
start: position,
end: position,
});
}
return current;
});

function getRanges(position: Position): Range[] {
const offset = document.offsetAt(position);
const result: Range[] = [];
for (const ymlDoc of doc.documents) {
let currentNode: ASTNode;
let firstNodeOffset: number;
let isFirstNode = true;
ymlDoc.visit((node) => {
const endOffset = node.offset + node.length;
// Skip if end offset doesn't even reach cursor position
if (endOffset < offset) {
return true;
}
let startOffset = node.offset;
// Recheck start offset with the trimmed one in case of this
// key:
// - value
// ↑
if (startOffset > offset) {
const nodePosition = document.positionAt(startOffset);
if (nodePosition.line !== position.line) {
return true;
}
const lineBeginning = { line: nodePosition.line, character: 0 };
const text = document.getText({
start: lineBeginning,
end: nodePosition,
});
if (text.trim().length !== 0) {
return true;
}
startOffset = document.offsetAt(lineBeginning);
if (startOffset > offset) {
return true;
}
}
// Allow equal for children to override
if (!currentNode || startOffset >= currentNode.offset) {
currentNode = node;
firstNodeOffset = startOffset;
}
return true;
});
while (currentNode) {
const startOffset = isFirstNode ? firstNodeOffset : currentNode.offset;
const endOffset = currentNode.offset + currentNode.length;
const range = {
start: document.positionAt(startOffset),
end: document.positionAt(endOffset),
};
const text = document.getText(range);
const trimmedText = text.trimEnd();
const trimmedLength = text.length - trimmedText.length;
if (trimmedLength > 0) {
range.end = document.positionAt(endOffset - trimmedLength);
}
// Add a jump between '' "" {} []
const isSurroundedBy = (startCharacter: string, endCharacter?: string): boolean => {
return trimmedText.startsWith(startCharacter) && trimmedText.endsWith(endCharacter || startCharacter);
};
if (
(currentNode.type === 'string' && (isSurroundedBy("'") || isSurroundedBy('"'))) ||
(currentNode.type === 'object' && isSurroundedBy('{', '}')) ||
(currentNode.type === 'array' && isSurroundedBy('[', ']'))
) {
result.push({
start: document.positionAt(startOffset + 1),
end: document.positionAt(endOffset - 1),
});
}
result.push(range);
currentNode = currentNode.parent;
isFirstNode = false;
}
// A position can't be in multiple documents
if (result.length > 0) {
break;
}
}
return result.reverse();
}
}
4 changes: 4 additions & 0 deletions src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
DocumentLink,
CodeLens,
DefinitionLink,
SelectionRange,
} from 'vscode-languageserver-types';
import { JSONSchema } from './jsonSchema';
import { YAMLDocumentSymbols } from './services/documentSymbols';
Expand Down Expand Up @@ -52,6 +53,7 @@ import { yamlDocumentsCache } from './parser/yaml-documents';
import { SettingsState } from '../yamlSettings';
import { JSONSchemaSelection } from '../languageserver/handlers/schemaSelectionHandlers';
import { YamlDefinition } from './services/yamlDefinition';
import { getSelectionRanges } from './services/yamlSelectionRanges';

export enum SchemaPriority {
SchemaStore = 1,
Expand Down Expand Up @@ -179,6 +181,7 @@ export interface LanguageService {
deleteSchemaContent(schemaDeletions: SchemaDeletions): void;
deleteSchemasWhole(schemaDeletions: SchemaDeletionsAll): void;
getFoldingRanges(document: TextDocument, context: FoldingRangesContext): FoldingRange[] | null;
getSelectionRanges(document: TextDocument, positions: Position[]): SelectionRange[] | undefined;
getCodeAction(document: TextDocument, params: CodeActionParams): CodeAction[] | undefined;
getCodeLens(document: TextDocument): Thenable<CodeLens[] | undefined> | CodeLens[] | undefined;
resolveCodeLens(param: CodeLens): Thenable<CodeLens> | CodeLens;
Expand Down Expand Up @@ -260,6 +263,7 @@ export function getLanguageService(params: {
return schemaService.deleteSchemas(schemaDeletions);
},
getFoldingRanges,
getSelectionRanges,
getCodeAction: (document, params) => {
return yamlCodeActions.getCodeAction(document, params);
},
Expand Down
1 change: 1 addition & 0 deletions src/yamlServerInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class YAMLServerInit {
definitionProvider: true,
documentLinkProvider: {},
foldingRangeProvider: true,
selectionRangeProvider: true,
codeActionProvider: true,
codeLensProvider: {
resolveProvider: false,
Expand Down
Loading

0 comments on commit 7a66521

Please sign in to comment.