Skip to content

Commit

Permalink
add proposed api for smart select, #63935
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Dec 3, 2018
1 parent c98725a commit 07be58b
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/vs/editor/common/modes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ export interface SelectionRangeProvider {
/**
* Provide ranges that should be selected from the given position.
*/
provideSelectionRanges(model: model.ITextModel, position: Position, token: CancellationToken): ProviderResult<Range[]>;
provideSelectionRanges(model: model.ITextModel, position: Position, token: CancellationToken): ProviderResult<IRange[]>;
}

export interface FoldingContext {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/contrib/smartSelect/smartSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export function provideSelectionRanges(model: ITextModel, position: Position, to
if (arrays.isNonEmptyArray(res)) {
for (const range of res) {
if (Range.isIRange(range) && Range.containsPosition(range, position)) {
ranges.push({ range, rank });
ranges.push({ range: Range.lift(range), rank });
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5303,7 +5303,7 @@ declare namespace monaco.languages {
/**
* Provide ranges that should be selected from the given position.
*/
provideSelectionRanges(model: editor.ITextModel, position: Position, token: CancellationToken): ProviderResult<Range[]>;
provideSelectionRanges(model: editor.ITextModel, position: Position, token: CancellationToken): ProviderResult<IRange[]>;
}

export interface FoldingContext {
Expand Down
12 changes: 12 additions & 0 deletions src/vs/vscode.proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ declare module 'vscode' {
export function sampleFunction(): Thenable<any>;
}

//#region Joh - selection range provider

export interface SelectionRangeProvider {
provideSelectionRanges(document: TextDocument, position: Position, token: CancellationToken): ProviderResult<Range[]>;
}

export namespace languages {
export function registerSelectionRangeProvider(selector: DocumentSelector, provider: SelectionRangeProvider): Disposable;
}

//#endregion

//#region Joh - read/write in chunks

export interface FileSystemProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,16 @@ export class MainThreadLanguageFeatures implements MainThreadLanguageFeaturesSha
});
}

// -- smart select

$registerSelectionRangeProvider(handle: number, selector: ISerializedDocumentFilter[]): void {
this._registrations[handle] = modes.SelectionRangeRegistry.register(typeConverters.LanguageSelector.from(selector), {
provideSelectionRanges: (model, position, token) => {
return this._proxy.$provideSelectionRanges(handle, model.uri, position, token);
}
});
}

// --- configuration

private static _reviveRegExp(regExp: ISerializedRegExp): RegExp {
Expand Down
3 changes: 3 additions & 0 deletions src/vs/workbench/api/node/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ export function createApiFactory(
registerFoldingRangeProvider(selector: vscode.DocumentSelector, provider: vscode.FoldingRangeProvider): vscode.Disposable {
return extHostLanguageFeatures.registerFoldingRangeProvider(extension, checkSelector(selector), provider);
},
registerSelectionRangeProvider(selector: vscode.DocumentSelector, provider: vscode.SelectionRangeProvider): vscode.Disposable {
return extHostLanguageFeatures.registerSelectionRangeProvider(extension, selector, provider);
},
setLanguageConfiguration: (language: string, configuration: vscode.LanguageConfiguration): vscode.Disposable => {
return extHostLanguageFeatures.setLanguageConfiguration(language, configuration);
}
Expand Down
2 changes: 2 additions & 0 deletions src/vs/workbench/api/node/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ export interface MainThreadLanguageFeaturesShape extends IDisposable {
$registerDocumentLinkProvider(handle: number, selector: ISerializedDocumentFilter[]): void;
$registerDocumentColorProvider(handle: number, selector: ISerializedDocumentFilter[]): void;
$registerFoldingRangeProvider(handle: number, selector: ISerializedDocumentFilter[]): void;
$registerSelectionRangeProvider(handle: number, selector: ISerializedDocumentFilter[]): void;
$setLanguageConfiguration(handle: number, languageId: string, configuration: ISerializedLanguageConfiguration): void;
}

Expand Down Expand Up @@ -885,6 +886,7 @@ export interface ExtHostLanguageFeaturesShape {
$provideDocumentColors(handle: number, resource: UriComponents, token: CancellationToken): Thenable<IRawColorInfo[]>;
$provideColorPresentations(handle: number, resource: UriComponents, colorInfo: IRawColorInfo, token: CancellationToken): Thenable<modes.IColorPresentation[]>;
$provideFoldingRanges(handle: number, resource: UriComponents, context: modes.FoldingContext, token: CancellationToken): Thenable<modes.FoldingRange[]>;
$provideSelectionRanges(handle: number, resource: UriComponents, position: IPosition, token: CancellationToken): Thenable<IRange[]>;
}

export interface ExtHostQuickOpenShape {
Expand Down
34 changes: 33 additions & 1 deletion src/vs/workbench/api/node/extHostLanguageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,11 +850,31 @@ class FoldingProviderAdapter {
}
}

class SelectionRangeAdapter {

constructor(
private readonly _documents: ExtHostDocuments,
private readonly _provider: vscode.SelectionRangeProvider
) { }

provideSelectionRanges(resource: URI, position: IPosition, token: CancellationToken): Promise<IRange[]> {
const { document } = this._documents.getDocumentData(resource);
const pos = typeConvert.Position.to(position);
return asThenable(() => this._provider.provideSelectionRanges(document, pos, token)).then(ranges => {
if (isNonEmptyArray(ranges)) {
return ranges.map(typeConvert.Range.from);
} else {
return undefined;
}
});
}
}

type Adapter = OutlineAdapter | CodeLensAdapter | DefinitionAdapter | HoverAdapter
| DocumentHighlightAdapter | ReferenceAdapter | CodeActionAdapter | DocumentFormattingAdapter
| RangeFormattingAdapter | OnTypeFormattingAdapter | NavigateTypeAdapter | RenameAdapter
| SuggestAdapter | SignatureHelpAdapter | LinkProviderAdapter | ImplementationAdapter | TypeDefinitionAdapter
| ColorProviderAdapter | FoldingProviderAdapter | DeclarationAdapter;
| ColorProviderAdapter | FoldingProviderAdapter | DeclarationAdapter | SelectionRangeAdapter;

class AdapterData {
constructor(
Expand Down Expand Up @@ -1248,6 +1268,18 @@ export class ExtHostLanguageFeatures implements ExtHostLanguageFeaturesShape {
return this._withAdapter(handle, FoldingProviderAdapter, adapter => adapter.provideFoldingRanges(URI.revive(resource), context, token));
}

// --- smart select

registerSelectionRangeProvider(extension: IExtensionDescription, selector: vscode.DocumentSelector, provider: vscode.SelectionRangeProvider): vscode.Disposable {
const handle = this._addNewAdapter(new SelectionRangeAdapter(this._documents, provider), extension);
this._proxy.$registerSelectionRangeProvider(handle, this._transformDocumentSelector(selector));
return this._createDisposable(handle);
}

$provideSelectionRanges(handle: number, resource: UriComponents, position: IPosition, token: CancellationToken): Thenable<IRange[]> {
return this._withAdapter(handle, SelectionRangeAdapter, adapter => adapter.provideSelectionRanges(URI.revive(resource), position, token));
}

// --- configuration

private static _serializeRegExp(regExp: RegExp): ISerializedRegExp {
Expand Down

0 comments on commit 07be58b

Please sign in to comment.