Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aqua-vscode): Prettify types view [LNG-331] #85

Merged
merged 5 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"url": "https://github.com/fluencelabs/aqua"
},
"dependencies": {
"@fluencelabs/aqua-language-server-api": "0.14.0",
"@fluencelabs/aqua-language-server-api": "0.14.1",
"global-dirs": "^3.0.0",
"vscode-languageserver": "^7.0.0",
"vscode-languageserver-textdocument": "^1.0.4",
Expand All @@ -25,4 +25,4 @@
"test": "ts-mocha",
"get-root": "npm root --global"
}
}
}
4 changes: 3 additions & 1 deletion server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { FluenceCli } from './cli';
import { Settings, SettingsManager } from './settings';
import { InfoManager } from './info';
import { tokenToLocation } from './utils';
import { typeToString } from './types';

// Create a connection to the server, using Node's IPC as a transport.
// Also include all preview / proposed LSP features.
Expand Down Expand Up @@ -122,7 +123,8 @@ connection.onHover(({ textDocument, position }: HoverParams): Hover | null => {

const info = documentInfos.infoAt(textDocument.uri, position);
if (info) {
const content: MarkupContent = { kind: MarkupKind.PlainText, value: info.type };
const typeStr = typeToString(info.type);
const content: MarkupContent = { kind: MarkupKind.PlainText, value: typeStr };
return { contents: content };
}

Expand Down
6 changes: 5 additions & 1 deletion server/src/test/info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Location, Position, Range } from 'vscode-languageserver';
import { compileAqua } from '../validation';
import { DocumentInfo } from '../info';
import { pathToUri, tokenToLocation } from '../utils';
import { ScalarType } from '@fluencelabs/aqua-language-server-api/aqua-lsp-api';

/**
* Load document from file, compile it and return info
Expand Down Expand Up @@ -77,7 +78,10 @@ describe('DocumentInfo Test Suite', () => {
const info = docInfo.infoAt(pos);

assert.ok(info, 'Info not found');
assert.strictEqual(info.type, 'string', 'Wrong type info');

const type = info.type as ScalarType;
assert.strictEqual(type.tag, 'scalar', 'Wrong type tag');
assert.strictEqual(type.name, 'string', 'Wrong type');
}
}
});
Expand Down
81 changes: 81 additions & 0 deletions server/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import type {
AbilityType,
ArrayType,
CanonStreamType,
OptionType,
ServiceType,
StreamMapType,
StreamType,
StructType,
Type,
} from '@fluencelabs/aqua-language-server-api/aqua-lsp-api';

const collectionPrefix = {
array: '[]',
stream: '*',
canon: '#',
streammap: '%',
option: '?',
};

function collectionToString(
type: ArrayType | StreamType | CanonStreamType | StreamMapType | OptionType,
depth: number,
) {
const elementType = typeToStringInner(type.element, depth);
const prefix = collectionPrefix[type.tag];
return prefix + elementType;
}

function namedToString(type: AbilityType | StructType | ServiceType, depth: number) {
let fieldsStr = Object.entries(type.fields).map(function ([name, fieldType]) {
const fieldTypeStr = typeToStringInner(fieldType, depth + 1);
return `${name}: ${fieldTypeStr}`;
});
return `${type.name}(${fieldsStr.join(', ')})`;
}

function typeToStringInner(type: Type, depth: number): string {
switch (type.tag) {
case 'nil':
return '∅';
case 'array':
case 'option':
case 'stream':
case 'streammap':
case 'canon':
return collectionToString(type, depth);
case 'struct':
case 'ability':
case 'service':
if (depth >= 1) {
return type.name;
}
return namedToString(type, depth);
case 'labeled':
const args = Object.entries(type.args).map(function ([name, argType]) {
const fieldTypeStr = typeToStringInner(argType, depth + 1);
return `${name}: ${fieldTypeStr}`;
});
return args.join(', ');
case 'unlabeled':
return type.types.map((t) => typeToStringInner(t, depth + 1)).join(', ');
case 'arrow':
const domainStr = typeToStringInner(type.domain, depth + 1);
const codomainStr = typeToStringInner(type.codomain, depth + 1);
return `func (${domainStr}) -> ${codomainStr}`;
case 'top':
return 'top';
case 'bottom':
return 'bottom';
case 'scalar':
return type.name;
default:
const _exhaustiveCheck: never = type;
return _exhaustiveCheck;
}
}

export function typeToString(type: Type): string {
return typeToStringInner(type, 0);
}
Loading