Skip to content

Commit

Permalink
monaco folding api (for #47808)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli committed Apr 18, 2018
1 parent 7bc218f commit 45e859b
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 29 deletions.
18 changes: 2 additions & 16 deletions src/vs/editor/common/modes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,30 +830,19 @@ export interface DocumentColorProvider {
*/
provideColorPresentations(model: model.ITextModel, colorInfo: IColorInformation, token: CancellationToken): IColorPresentation[] | Thenable<IColorPresentation[]>;
}

/**
* @internal
*/
export interface FoldingContext {
}

/**
* A provider of colors for editor models.
*/
/**
* @internal
*/
export interface FoldingRangeProvider {
/**
* Provides the color ranges for a specific model.
*/
provideFoldingRanges(model: model.ITextModel, context: FoldingContext, token: CancellationToken): IFoldingRange[] | Thenable<IFoldingRange[]>;
provideFoldingRanges(model: model.ITextModel, context: FoldingContext, token: CancellationToken): FoldingRange[] | Thenable<FoldingRange[]>;
}

/**
* @internal
*/
export interface IFoldingRange {
export interface FoldingRange {

/**
* The zero-based start line of the range to fold. The folded area starts after the line's last character.
Expand All @@ -873,9 +862,6 @@ export interface IFoldingRange {
*/
kind?: FoldingRangeKind;
}
/**
* @internal
*/
export class FoldingRangeKind {
/**
* Kind for folding range representing a comment. The value of the kind is 'comment'.
Expand Down
4 changes: 2 additions & 2 deletions src/vs/editor/contrib/folding/syntaxRangeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

import { FoldingRangeProvider, IFoldingRange, FoldingContext } from 'vs/editor/common/modes';
import { FoldingRangeProvider, FoldingRange, FoldingContext } from 'vs/editor/common/modes';
import { onUnexpectedExternalError } from 'vs/base/common/errors';
import { toThenable } from 'vs/base/common/async';
import { ITextModel } from 'vs/editor/common/model';
Expand All @@ -16,7 +16,7 @@ import { CancellationToken } from 'vs/base/common/cancellation';

const MAX_FOLDING_REGIONS_FOR_INDENT_LIMIT = 5000;

export interface IFoldingRangeData extends IFoldingRange {
export interface IFoldingRangeData extends FoldingRange {
rank: number;
}

Expand Down
13 changes: 7 additions & 6 deletions src/vs/editor/standalone/browser/standaloneLanguages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,11 @@ export function registerColorProvider(languageId: string, provider: modes.Docume
}

/**
* Register a folding provider
* Register a folding range provider
*/
/*export function registerFoldingProvider(languageId: string, provider: modes.FoldingProvider): IDisposable {
return modes.FoldingProviderRegistry.register(languageId, provider);
}*/

export function registerFoldingRangeProvider(languageId: string, provider: modes.FoldingRangeProvider): IDisposable {
return modes.FoldingRangeProviderRegistry.register(languageId, provider);
}

/**
* Contains additional diagnostic information about the context in which
Expand Down Expand Up @@ -787,12 +786,14 @@ export function createMonacoLanguagesAPI(): typeof monaco.languages {
registerOnTypeFormattingEditProvider: registerOnTypeFormattingEditProvider,
registerLinkProvider: registerLinkProvider,
registerColorProvider: registerColorProvider,
registerFoldingRangeProvider: registerFoldingRangeProvider,

// enums
DocumentHighlightKind: modes.DocumentHighlightKind,
CompletionItemKind: CompletionItemKind,
SymbolKind: modes.SymbolKind,
IndentAction: IndentAction,
SuggestTriggerKind: modes.SuggestTriggerKind
SuggestTriggerKind: modes.SuggestTriggerKind,
FoldingRangeKind: modes.FoldingRangeKind
};
}
58 changes: 57 additions & 1 deletion src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4105,8 +4105,10 @@ declare namespace monaco.languages {
export function registerColorProvider(languageId: string, provider: DocumentColorProvider): IDisposable;

/**
* Register a folding provider
* Register a folding range provider
*/
export function registerFoldingRangeProvider(languageId: string, provider: FoldingRangeProvider): IDisposable;

/**
* Contains additional diagnostic information about the context in which
* a [code action](#CodeActionProvider.provideCodeActions) is run.
Expand Down Expand Up @@ -5000,6 +5002,60 @@ declare namespace monaco.languages {
provideColorPresentations(model: editor.ITextModel, colorInfo: IColorInformation, token: CancellationToken): IColorPresentation[] | Thenable<IColorPresentation[]>;
}

export interface FoldingContext {
}

/**
* A provider of colors for editor models.
*/
export interface FoldingRangeProvider {
/**
* Provides the color ranges for a specific model.
*/
provideFoldingRanges(model: editor.ITextModel, context: FoldingContext, token: CancellationToken): FoldingRange[] | Thenable<FoldingRange[]>;
}

export interface FoldingRange {
/**
* The zero-based start line of the range to fold. The folded area starts after the line's last character.
*/
start: number;
/**
* The zero-based end line of the range to fold. The folded area ends with the line's last character.
*/
end: number;
/**
* Describes the [Kind](#FoldingRangeKind) of the folding range such as [Comment](#FoldingRangeKind.Comment) or
* [Region](#FoldingRangeKind.Region). The kind is used to categorize folding ranges and used by commands
* like 'Fold all comments'. See
* [FoldingRangeKind](#FoldingRangeKind) for an enumeration of standardized kinds.
*/
kind?: FoldingRangeKind;
}

export class FoldingRangeKind {
value: string;
/**
* Kind for folding range representing a comment. The value of the kind is 'comment'.
*/
static readonly Comment: FoldingRangeKind;
/**
* Kind for folding range representing a import. The value of the kind is 'imports'.
*/
static readonly Imports: FoldingRangeKind;
/**
* Kind for folding range representing regions (for example marked by `#region`, `#endregion`).
* The value of the kind is 'region'.
*/
static readonly Region: FoldingRangeKind;
/**
* Creates a new [FoldingRangeKind](#FoldingRangeKind).
*
* @param value of the kind.
*/
constructor(value: string);
}

export interface ResourceFileEdit {
oldUri: Uri;
newUri: Uri;
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/node/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ export interface ExtHostLanguageFeaturesShape {
$resolveDocumentLink(handle: number, link: modes.ILink): TPromise<modes.ILink>;
$provideDocumentColors(handle: number, resource: UriComponents): TPromise<IRawColorInfo[]>;
$provideColorPresentations(handle: number, resource: UriComponents, colorInfo: IRawColorInfo): TPromise<modes.IColorPresentation[]>;
$provideFoldingRanges(handle: number, resource: UriComponents, context: modes.FoldingContext): TPromise<modes.IFoldingRange[]>;
$provideFoldingRanges(handle: number, resource: UriComponents, context: modes.FoldingContext): TPromise<modes.FoldingRange[]>;
}

export interface ExtHostQuickOpenShape {
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/api/node/extHostLanguageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ class FoldingProviderAdapter {
private _provider: vscode.FoldingRangeProvider
) { }

provideFoldingRanges(resource: URI, context: modes.FoldingContext): TPromise<modes.IFoldingRange[]> {
provideFoldingRanges(resource: URI, context: modes.FoldingContext): TPromise<modes.FoldingRange[]> {
const doc = this._documents.getDocumentData(resource).document;
return asWinJsPromise(token => this._provider.provideFoldingRanges(doc, context, token)).then(ranges => {
if (!Array.isArray(ranges)) {
Expand Down Expand Up @@ -1184,7 +1184,7 @@ export class ExtHostLanguageFeatures implements ExtHostLanguageFeaturesShape {
return this._createDisposable(handle);
}

$provideFoldingRanges(handle: number, resource: UriComponents, context: vscode.FoldingContext): TPromise<modes.IFoldingRange[]> {
$provideFoldingRanges(handle: number, resource: UriComponents, context: vscode.FoldingContext): TPromise<modes.FoldingRange[]> {
return this._withAdapter(handle, FoldingProviderAdapter, adapter => adapter.provideFoldingRanges(URI.revive(resource), context));
}

Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/node/extHostTypeConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ export namespace ProgressLocation {
}

export namespace FoldingRange {
export function from(r: vscode.FoldingRange): modes.IFoldingRange {
export function from(r: vscode.FoldingRange): modes.FoldingRange {
return { start: r.start + 1, end: r.end + 1, kind: r.kind };
}
}
Expand Down

0 comments on commit 45e859b

Please sign in to comment.