Skip to content

Commit

Permalink
feat: go to definition
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanba committed Mar 15, 2024
1 parent a480bc1 commit 8f05015
Show file tree
Hide file tree
Showing 15 changed files with 247 additions and 38 deletions.
12 changes: 6 additions & 6 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@
"editor.tabSize": 2,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
}
},
"[typescript]": {
"editor.tabSize": 2,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
}
},
"[javascriptreact]": {
"editor.tabSize": 2,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
}
},
"[javascript]": {
"editor.tabSize": 2,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
}
},
"[json]": {
Expand All @@ -89,14 +89,14 @@
"editor.tabSize": 2,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.stylelint": true
"source.fixAll.stylelint": "explicit"
}
},
"[less]": {
"editor.tabSize": 2,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.stylelint": true
"source.fixAll.stylelint": "explicit"
}
},
"[css][less]": {
Expand Down
4 changes: 2 additions & 2 deletions packages/libro-cofine-editor/src/libro-e2-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -889,8 +889,8 @@ export class LibroE2Editor implements IEditor {
};
setCursorPosition = (position: IPosition) => {
this.monacoEditor?.setPosition({
column: position.column,
lineNumber: position.line,
column: position.column + 1,
lineNumber: position.line + 1,
});
};
getSelection = () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/libro-core/src/cell/libro-cell-model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Model } from '@difizen/libro-code-editor';
import type { ICell } from '@difizen/libro-common';
import { concatMultilineString } from '@difizen/libro-common';
import { DisposableCollection } from '@difizen/mana-app';
import { DisposableCollection, watch } from '@difizen/mana-app';
import { prop, inject, postConstruct, transient } from '@difizen/mana-app';

import type { DefaultDecodedFormatter } from '../formatter/index.js';
Expand Down Expand Up @@ -34,6 +34,8 @@ export class LibroCellModel extends Model implements CellModel {
@prop()
trusted: boolean;

version = 0;

constructor(@inject(CellOptions) options: CellOptions) {
super({
id: options.cell.id as string,
Expand Down Expand Up @@ -68,6 +70,9 @@ export class LibroCellModel extends Model implements CellModel {
this.libroFormatType,
formatValue,
);
watch(this, 'value', () => {
this.version++;
});
}

get source(): string {
Expand Down
3 changes: 3 additions & 0 deletions packages/libro-core/src/libro-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export class LibroModel implements NotebookModel, DndListModel {

id: string;

version = 0;

/**
* The shared notebook model.
*/
Expand Down Expand Up @@ -298,6 +300,7 @@ export class LibroModel implements NotebookModel, DndListModel {
}

onChange() {
this.version++;
this.dirty = true;
this.onChangedEmitter.fire(true);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/libro-core/src/libro-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export interface ICellContentChange {
}
export interface BaseNotebookModel {
id: string;
version: number;
/**
* The dirty state of the model.
* #### Notes
Expand Down Expand Up @@ -322,6 +323,8 @@ export interface CellModel extends IModel, Disposable {
*/
id: string;

version: number;

source: string;

/**
Expand Down
7 changes: 3 additions & 4 deletions packages/libro-jupyter/src/cell/jupyter-code-cell-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
TooltipProviderOption,
} from '@difizen/libro-code-editor';
import { KernelError } from '@difizen/libro-kernel';
import { LibroCellURIScheme } from '@difizen/libro-language-client';
import { getCellURI } from '@difizen/libro-language-client';
import { transient, URI } from '@difizen/mana-app';
import { view, ViewInstance } from '@difizen/mana-app';
import { getOrigin, useInject } from '@difizen/mana-app';
Expand Down Expand Up @@ -55,9 +55,8 @@ export class JupyterCodeCellView extends LibroCodeCellView {

protected override getEditorOption(): CodeEditorViewOptions {
const options = super.getEditorOption();
let uri = new URI(this.parent.model.filePath);
uri = URI.withScheme(uri, LibroCellURIScheme);
uri = URI.withQuery(uri, `cellid=${this.model.id}`);
const uri = getCellURI(this.parent.model, this.model);

return {
...options,
uuid: uri.toString(),
Expand Down
14 changes: 1 addition & 13 deletions packages/libro-jupyter/src/libro-jupyter-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import type { VirtualizedManager } from '@difizen/libro-core';
import { LibroModel, VirtualizedManagerHelper } from '@difizen/libro-core';
import {
ContentsManager,
ExecutableNotebookModel,
isDisplayDataMsg,
LibroKernelConnectionManager,
ServerConnection,
ServerManager,
} from '@difizen/libro-kernel';
import type { IKernelConnection } from '@difizen/libro-kernel';
import type { IKernelConnection, ExecutableNotebookModel } from '@difizen/libro-kernel';
import type { IContentsCheckpointModel, IContentsModel } from '@difizen/libro-kernel';
import { getOrigin, ModalService, prop } from '@difizen/mana-app';
import { Deferred } from '@difizen/mana-app';
Expand All @@ -26,17 +25,6 @@ import { getDefaultKernel } from './utils/index.js';
type IModel = IContentsModel;
@transient()
export class LibroJupyterModel extends LibroModel implements ExecutableNotebookModel {
static is = (arg: Record<any, any> | undefined): arg is LibroJupyterModel => {
return (
!!arg &&
ExecutableNotebookModel.is(arg) &&
'kernelConnection' in arg &&
typeof (arg as any).kernelConnection === 'object' &&
'lspEnabled' in arg &&
typeof (arg as any).lspEnabled === 'boolean'
);
};

protected libroFileService: LibroFileService;
protected virtualizedManager: VirtualizedManager;

Expand Down
2 changes: 1 addition & 1 deletion packages/libro-lab/src/lab-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class LibroLabApp implements ApplicationContribution {
@inject(LayoutService) layoutService: LayoutService;

async onStart() {
localStorage.setItem(ShouldPreventStoreViewKey, 'false');
localStorage.setItem(ShouldPreventStoreViewKey, 'true');
this.configurationService.set(
LibroJupyterConfiguration['OpenSlot'],
LibroLabLayoutSlots.content,
Expand Down
1 change: 1 addition & 0 deletions packages/libro-language-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
},
"dependencies": {
"@difizen/libro-core": "^0.1.23",
"@difizen/libro-code-editor": "^0.1.23",
"@difizen/libro-kernel": "^0.1.23",
"@difizen/libro-common": "^0.1.23",
"@difizen/libro-lsp": "^0.1.23",
Expand Down
76 changes: 76 additions & 0 deletions packages/libro-language-client/src/common/codeConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* ------------------------------------------------------------------------------------------ */

import * as proto from '@difizen/vscode-languageserver-protocol';
import type { LocationLink } from 'vscode';
import type {
InlineCompletionContext,
InlayHint,
Expand Down Expand Up @@ -46,6 +47,8 @@ import type {
TextEdit,
Uri,
SymbolInformation as VSymbolInformation,
Definition,
DefinitionLink,
} from 'vscode';

import ProtocolCallHierarchyItem from './protocolCallHierarchyItem.js';
Expand Down Expand Up @@ -172,11 +175,25 @@ export interface Converter {

asRanges(values: readonly Range[]): proto.Range[];

asDefinitionResult(item: Definition): proto.Definition;
asDefinitionResult(item: DefinitionLink[]): proto.Definition;
asDefinitionResult(item: undefined | null): undefined;
asDefinitionResult(
item: Definition | DefinitionLink[] | undefined | null,
): proto.Definition | undefined;
asDefinitionResult(
item: Definition | DefinitionLink[] | undefined | null,
): proto.Definition | undefined;

asLocation(value: null): null;
asLocation(value: undefined): undefined;
asLocation(value: Location): proto.Location;
asLocation(value: Location | undefined | null): proto.Location | undefined | null;

asLocationLink(item: undefined | null): undefined;
asLocationLink(item: LocationLink): proto.LocationLink;
asLocationLink(item: LocationLink | undefined | null): proto.LocationLink | undefined;

asDiagnosticSeverity(value: DiagnosticSeverity): number;
asDiagnosticTag(value: DiagnosticTag): number | undefined;

Expand Down Expand Up @@ -623,6 +640,63 @@ export function createConverter(uriConverter?: URIConverter): Converter {
return values.map(asRange as (item: Range) => proto.Range);
}

function asLocationLink(item: undefined | null): undefined;
function asLocationLink(item: LocationLink): proto.LocationLink;
function asLocationLink(
item: LocationLink | undefined | null,
): proto.LocationLink | undefined {
if (!item) {
return undefined;
}
const result: proto.LocationLink = {
targetUri: item.targetUri.toString(),
targetRange: asRange(item.targetSelectionRange)!, // See issue: https://github.com/Microsoft/vscode/issues/58649
originSelectionRange: asRange(item.originSelectionRange)!,
targetSelectionRange: asRange(item.targetSelectionRange)!,
};
if (!result.targetSelectionRange) {
throw new Error(`targetSelectionRange must not be undefined or null`);
}
return result;
}

// Function to check if an object is a LocationLink
function isLocationLink(object: any): object is LocationLink {
return (
object !== undefined &&
'targetUri' in object &&
'targetRange' in object &&
'targetSelectionRange' in object
);
}

function asDefinitionResult(item: Definition): proto.Definition;
function asDefinitionResult(item: DefinitionLink[]): proto.Definition;
function asDefinitionResult(item: undefined | null): undefined;
function asDefinitionResult(
item: Definition | DefinitionLink[] | undefined | null,
): proto.Definition | proto.DefinitionLink[] | undefined;
function asDefinitionResult(
item: Definition | DefinitionLink[] | undefined | null,
): proto.Definition | proto.DefinitionLink[] | undefined {
if (!item) {
return undefined;
}
if (Array.isArray(item)) {
if (item.length === 0) {
return undefined;
} else if (isLocationLink(item[0])) {
const links: LocationLink[] = item as unknown as LocationLink[];
return links.map((location) => asLocationLink(location));
} else {
const locations: Location[] = item as Location[];
return locations.map((location) => asLocation(location));
}
} else {
return asLocation(item);
}
}

function asLocation(value: Location): proto.Location;
function asLocation(value: undefined): undefined;
function asLocation(value: null): null;
Expand Down Expand Up @@ -1405,5 +1479,7 @@ export function createConverter(uriConverter?: URIConverter): Converter {
asWorkspaceSymbol,
asInlineCompletionParams,
asInlineCompletionContext,
asDefinitionResult,
asLocationLink,
};
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CellView, LibroView } from '@difizen/libro-core';
import { ExecutableNotebookModel } from '@difizen/libro-kernel';
import type { NotebookCell, NotebookDocument, NotebookRange } from 'vscode';

import { NotebookDocumentSyncFeature } from '../notebook.js';
Expand All @@ -9,14 +10,14 @@ import { EndOfLine, NotebookCellKind, Uri } from './vscodeAdaptor.js';
export const l2c = {
asNotebookDocument(libroView: LibroView): NotebookDocument {
const model = libroView.model as any;
if (model.filePath === undefined) {
throw new Error('no filePath: invalid libro jupyter model');
if (!ExecutableNotebookModel.is(model)) {
throw new Error('invalid libro jupyter model');
}
const filePath = model.filePath as string;
return {
uri: Uri.parse(filePath),
notebookType: 'jupyter',
version: 0,
version: libroView.model.version,
isDirty: libroView.model.dirty,
isUntitled: false,
isClosed: false,
Expand Down Expand Up @@ -49,7 +50,7 @@ export const l2c = {
fileName: filePath,
isUntitled: false,
languageId: 'python',
version: 0,
version: cell.model.version,
isDirty: false,
isClosed: false,
save: unsupported,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,25 @@ export class MonacoLanguages implements IMonacoLanguages {
selector: DocumentSelector,
provider: DefinitionProvider,
): Disposable {
return monaco.languages.registerDefinitionProvider(
selector,
this.createDefinitionProvider(provider),
);
}
protected createDefinitionProvider(
provider: DefinitionProvider,
): monaco.languages.DefinitionProvider {
return {
dispose: () => {
return;
provideDefinition: async (model, position, token) => {
const params = this.m2p.asTextDocumentPositionParams(model, position);
const result = await provider.provideDefinition(
{ uri: model.uri } as any,
this.p2c.asPosition(params.position),
token,
);
return (
result && this.p2m.asDefinitionResult(this.c2p.asDefinitionResult(result))
);
},
};
}
Expand Down
1 change: 1 addition & 0 deletions packages/libro-language-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './libro-language-client.js';
export * from './module.js';
export * from './common/api.js';
export * from './constants.js';
export * from './util.js';
Loading

0 comments on commit 8f05015

Please sign in to comment.