Skip to content

Commit

Permalink
add htmlLanguageService.getFoldingRanges
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli committed Apr 16, 2018
1 parent 343e75e commit cf7e3a5
Show file tree
Hide file tree
Showing 15 changed files with 591 additions and 209 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2.1.3 / 2018-04-16
==================
* Added API `htmlLanguageService.getFoldingRanges` returning folding ranges for the given document

2.1.0 / 2018-03-08
==================
* Added API `htmlLanguageService.setCompletionParticipants` that allows participation in code completion
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and the Monaco editor.
- *format* formats the code at the given range.
- *findDocumentLinks* finds all links in the document.
- *findDocumentSymbols* finds all the symbols in the document.
- *htmlLanguageService.getFoldingRanges* returning folding ranges for the given document

Installation
------------
Expand Down
118 changes: 7 additions & 111 deletions src/htmlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,118 +13,12 @@ import { findDocumentLinks } from './services/htmlLinks';
import { findDocumentHighlights } from './services/htmlHighlighting';
import { findDocumentSymbols } from './services/htmlSymbolsProvider';
import { TextDocument, Position, CompletionItem, CompletionList, Hover, Range, SymbolInformation, Diagnostic, TextEdit, DocumentHighlight, FormattingOptions, MarkedString, DocumentLink } from 'vscode-languageserver-types';
import { Scanner, HTMLDocument, CompletionConfiguration, ICompletionParticipant, HTMLFormatConfiguration, DocumentContext, FoldingRangeList } from './htmlLanguageTypes';
import { getFoldingRanges } from './services/htmlFolding';

export { TextDocument, Position, CompletionItem, CompletionList, Hover, Range, SymbolInformation, Diagnostic, TextEdit, DocumentHighlight, FormattingOptions, MarkedString, DocumentLink };
export * from './htmlLanguageTypes';
export * from 'vscode-languageserver-types';

export interface HTMLFormatConfiguration {
tabSize?: number;
insertSpaces?: boolean;
wrapLineLength?: number;
unformatted?: string;
contentUnformatted?: string;
indentInnerHtml?: boolean;
wrapAttributes?: 'auto' | 'force' | 'force-aligned' | 'force-expand-multiline';
preserveNewLines?: boolean;
maxPreserveNewLines?: number;
indentHandlebars?: boolean;
endWithNewline?: boolean;
extraLiners?: string;
}

export interface CompletionConfiguration {
[provider: string]: boolean | undefined;
hideAutoCompleteProposals?: boolean;
}

export interface Node {
tag: string | undefined;
start: number;
end: number;
endTagStart: number | undefined;
children: Node[];
parent?: Node;
attributes?: { [name: string]: string | null } | undefined;
}


export enum TokenType {
StartCommentTag,
Comment,
EndCommentTag,
StartTagOpen,
StartTagClose,
StartTagSelfClose,
StartTag,
EndTagOpen,
EndTagClose,
EndTag,
DelimiterAssign,
AttributeName,
AttributeValue,
StartDoctypeTag,
Doctype,
EndDoctypeTag,
Content,
Whitespace,
Unknown,
Script,
Styles,
EOS
}

export enum ScannerState {
WithinContent,
AfterOpeningStartTag,
AfterOpeningEndTag,
WithinDoctype,
WithinTag,
WithinEndTag,
WithinComment,
WithinScriptContent,
WithinStyleContent,
AfterAttributeName,
BeforeAttributeValue
}

export interface Scanner {
scan(): TokenType;
getTokenType(): TokenType;
getTokenOffset(): number;
getTokenLength(): number;
getTokenEnd(): number;
getTokenText(): string;
getTokenError(): string | undefined;
getScannerState(): ScannerState;
}

export declare type HTMLDocument = {
roots: Node[];
findNodeBefore(offset: number): Node;
findNodeAt(offset: number): Node;
};

export interface DocumentContext {
resolveReference(ref: string, base?: string): string;
}

export interface HtmlAttributeValueContext {
document: TextDocument;
position: Position;
tag: string;
attribute: string;
value: string;
range: Range;
}

export interface HtmlContentContext {
document: TextDocument;
position: Position;
}

export interface ICompletionParticipant {
onHtmlAttributeValue?: (context: HtmlAttributeValueContext) => void;
onHtmlContent?: (context: HtmlContentContext) => void;
}

export interface LanguageService {
createScanner(input: string, initialOffset?: number): Scanner;
Expand All @@ -137,6 +31,7 @@ export interface LanguageService {
findDocumentLinks(document: TextDocument, documentContext: DocumentContext): DocumentLink[];
findDocumentSymbols(document: TextDocument, htmlDocument: HTMLDocument): SymbolInformation[];
doTagComplete(document: TextDocument, position: Position, htmlDocument: HTMLDocument): string | null;
getFoldingRanges(document: TextDocument, context?: { maxRanges?: number }): FoldingRangeList;
}

export function getLanguageService(): LanguageService {
Expand All @@ -151,6 +46,7 @@ export function getLanguageService(): LanguageService {
findDocumentHighlights,
findDocumentLinks,
findDocumentSymbols,
getFoldingRanges,
doTagComplete: htmlCompletion.doTagComplete.bind(htmlCompletion),
};
}
}
166 changes: 166 additions & 0 deletions src/htmlLanguageTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';

import { TextDocument, Position, Range } from 'vscode-languageserver-types';


export interface HTMLFormatConfiguration {
tabSize?: number;
insertSpaces?: boolean;
wrapLineLength?: number;
unformatted?: string;
contentUnformatted?: string;
indentInnerHtml?: boolean;
wrapAttributes?: 'auto' | 'force' | 'force-aligned' | 'force-expand-multiline';
preserveNewLines?: boolean;
maxPreserveNewLines?: number;
indentHandlebars?: boolean;
endWithNewline?: boolean;
extraLiners?: string;
}

export interface CompletionConfiguration {
[provider: string]: boolean | undefined;
hideAutoCompleteProposals?: boolean;
}

export interface Node {
tag: string | undefined;
start: number;
end: number;
endTagStart: number | undefined;
children: Node[];
parent?: Node;
attributes?: { [name: string]: string | null } | undefined;
}


export enum TokenType {
StartCommentTag,
Comment,
EndCommentTag,
StartTagOpen,
StartTagClose,
StartTagSelfClose,
StartTag,
EndTagOpen,
EndTagClose,
EndTag,
DelimiterAssign,
AttributeName,
AttributeValue,
StartDoctypeTag,
Doctype,
EndDoctypeTag,
Content,
Whitespace,
Unknown,
Script,
Styles,
EOS
}

export enum ScannerState {
WithinContent,
AfterOpeningStartTag,
AfterOpeningEndTag,
WithinDoctype,
WithinTag,
WithinEndTag,
WithinComment,
WithinScriptContent,
WithinStyleContent,
AfterAttributeName,
BeforeAttributeValue
}

export interface Scanner {
scan(): TokenType;
getTokenType(): TokenType;
getTokenOffset(): number;
getTokenLength(): number;
getTokenEnd(): number;
getTokenText(): string;
getTokenError(): string | undefined;
getScannerState(): ScannerState;
}

export declare type HTMLDocument = {
roots: Node[];
findNodeBefore(offset: number): Node;
findNodeAt(offset: number): Node;
};

export interface DocumentContext {
resolveReference(ref: string, base?: string): string;
}

export interface HtmlAttributeValueContext {
document: TextDocument;
position: Position;
tag: string;
attribute: string;
value: string;
range: Range;
}

export interface HtmlContentContext {
document: TextDocument;
position: Position;
}

export interface ICompletionParticipant {
onHtmlAttributeValue?: (context: HtmlAttributeValueContext) => void;
onHtmlContent?: (context: HtmlContentContext) => void;
}


export interface FoldingRangeList {
/**
* The folding ranges.
*/
ranges: FoldingRange[];
}
export const enum FoldingRangeType {
/**
* Folding range for a comment
*/
Comment = "comment",
/**
* Folding range for a imports or includes
*/
Imports = "imports",
/**
* Folding range for a region (e.g. `#region`)
*/
Region = "region",
}

/**
* Represents a folding range.
*/
export interface FoldingRange {
/**
* The start line number of the folding range.
*/
startLine: number;
/**
* The start column of the folding range. If not set, this defaults to the length of the start line.
*/
startColumn?: number;
/**
* The end line number. The last line will be hidden.
*/
endLine: number;
/**
* The start column of the folding range. If not set, this defaults to the length of the end line.
*/
endColumn?: number;
/**
* The type of folding range.
*/
type?: FoldingRangeType | string;
}
3 changes: 2 additions & 1 deletion src/parser/htmlParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*--------------------------------------------------------------------------------------------*/
'use strict';

import { TokenType, createScanner } from './htmlScanner';
import { createScanner } from './htmlScanner';
import { findFirst } from '../utils/arrays';
import { isEmptyElement } from './htmlTags';
import { TokenType } from '../htmlLanguageTypes';

export class Node {
public tag: string | undefined;
Expand Down
Loading

0 comments on commit cf7e3a5

Please sign in to comment.