Skip to content
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"lint": "eslint \"packages/**/*.{ts,js}\""
},
"dependencies": {
"axios": "0.19.2"
"axios": "0.19.2",
"typescript": "^4.1.3"
},
"devDependencies": {
"@sveltejs/eslint-config": "github:sveltejs/eslint-config#v5.2.0",
Expand Down
19 changes: 8 additions & 11 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-language-server",
"version": "0.10.3",
"version": "0.11.0",
"description": "A language server for Svelte",
"main": "dist/src/index.js",
"typings": "dist/src/index",
Expand Down Expand Up @@ -50,21 +50,18 @@
"cosmiconfig": "^7.0.0",
"estree-walker": "^2.0.1",
"lodash": "^4.17.19",
"prettier": "2.1.2",
"prettier": "2.2.1",
"prettier-plugin-svelte": "~1.4.1",
"source-map": "^0.7.3",
"svelte": "3.28.0",
"svelte": "3.31.0",
"svelte-preprocess": "~4.6.1",
"svelte2tsx": "*",
"typescript": "*",
"vscode-css-languageservice": "4.2.0",
"vscode-emmet-helper": "1.2.17",
"vscode-html-languageservice": "3.1.4",
"vscode-languageserver": "6.1.1",
"vscode-languageserver-types": "3.15.1",
"vscode-css-languageservice": "5.0.0",
"vscode-emmet-helper": "2.1.2",
"vscode-html-languageservice": "4.0.0",
"vscode-languageserver": "7.0.0",
"vscode-languageserver-types": "3.16.0",
"vscode-uri": "2.1.2"
},
"resolutions": {
"vscode-languageserver-types": "3.15.1"
}
}
4 changes: 2 additions & 2 deletions packages/language-server/src/lib/DiagnosticsManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IConnection, TextDocumentIdentifier, Diagnostic } from 'vscode-languageserver';
import { _Connection, TextDocumentIdentifier, Diagnostic } from 'vscode-languageserver';
import { DocumentManager, Document } from './documents';

export type SendDiagnostics = IConnection['sendDiagnostics'];
export type SendDiagnostics = _Connection['sendDiagnostics'];
export type GetDiagnostics = (doc: TextDocumentIdentifier) => Thenable<Diagnostic[]>;

export class DiagnosticsManager {
Expand Down
41 changes: 35 additions & 6 deletions packages/language-server/src/lib/documents/DocumentMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
LocationLink,
TextDocumentEdit,
CodeAction,
SelectionRange
SelectionRange,
TextEdit,
InsertReplaceEdit
} from 'vscode-languageserver';
import { TagInformation, offsetAt, positionAt } from './utils';
import { SourceMapConsumer } from 'source-map';
Expand Down Expand Up @@ -206,10 +208,14 @@ export function mapRangeToOriginal(
fragment: Pick<DocumentMapper, 'getOriginalPosition'>,
range: Range
): Range {
return Range.create(
fragment.getOriginalPosition(range.start),
fragment.getOriginalPosition(range.end)
);
// DON'T use Range.create here! Positions might not be mapped
// and therefore return negative numbers, which makes Range.create throw.
// These invalid position need to be handled
// on a case-by-case basis in the calling functions.
return {
start: fragment.getOriginalPosition(range.start),
end: fragment.getOriginalPosition(range.end)
};
}

export function mapRangeToGenerated(fragment: DocumentMapper, range: Range): Range {
Expand All @@ -227,7 +233,10 @@ export function mapCompletionItemToOriginal(
return item;
}

return { ...item, textEdit: mapObjWithRangeToOriginal(fragment, item.textEdit) };
return {
...item,
textEdit: mapEditToOriginal(fragment, item.textEdit)
};
}

export function mapHoverToParent(
Expand All @@ -248,6 +257,26 @@ export function mapObjWithRangeToOriginal<T extends { range: Range }>(
return { ...objWithRange, range: mapRangeToOriginal(fragment, objWithRange.range) };
}

export function mapInsertReplaceEditToOriginal(
fragment: Pick<DocumentMapper, 'getOriginalPosition'>,
edit: InsertReplaceEdit
): InsertReplaceEdit {
return {
...edit,
insert: mapRangeToOriginal(fragment, edit.insert),
replace: mapRangeToOriginal(fragment, edit.replace)
};
}

export function mapEditToOriginal(
fragment: Pick<DocumentMapper, 'getOriginalPosition'>,
edit: TextEdit | InsertReplaceEdit
): TextEdit | InsertReplaceEdit {
return TextEdit.is(edit)
? mapObjWithRangeToOriginal(fragment, edit)
: mapInsertReplaceEditToOriginal(fragment, edit);
}

export function mapDiagnosticToGenerated(
fragment: DocumentMapper,
diagnostic: Diagnostic
Expand Down
8 changes: 4 additions & 4 deletions packages/language-server/src/ls-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { merge, get } from 'lodash';
import { UserPreferences } from 'typescript';
import { EmmetConfiguration } from 'vscode-emmet-helper';
import { VSCodeEmmetConfig } from 'vscode-emmet-helper';

/**
* Default config for the language server.
Expand Down Expand Up @@ -219,7 +219,7 @@ export class LSConfigManager {
}
};
private prettierConfig: any = {};
private emmetConfig: EmmetConfiguration = {};
private emmetConfig: VSCodeEmmetConfig = {};

/**
* Updates config.
Expand Down Expand Up @@ -268,11 +268,11 @@ export class LSConfigManager {
this.listeners.push(callback);
}

updateEmmetConfig(config: EmmetConfiguration): void {
updateEmmetConfig(config: VSCodeEmmetConfig): void {
this.emmetConfig = config || {};
}

getEmmetConfig(): EmmetConfiguration {
getEmmetConfig(): VSCodeEmmetConfig {
return this.emmetConfig;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/language-server/src/plugins/css/CSSPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class CSSPlugin
.getGlobalVars()
.map((globalVar) => ({
label: `var(${globalVar.name})`,
sortText: `-`,
sortText: '-',
detail: `${globalVar.filename}\n\n${globalVar.name}: ${globalVar.value}`,
kind: CompletionItemKind.Value
}));
Expand Down
11 changes: 7 additions & 4 deletions packages/language-server/src/plugins/html/HTMLPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,13 @@ export class HTMLPlugin implements HoverProvider, CompletionsProvider {
insertText:
existingCompletion.insertText &&
`${existingCompletion.insertText} lang="${lang}"`,
textEdit: existingCompletion.textEdit && {
range: existingCompletion.textEdit.range,
newText: `${existingCompletion.textEdit.newText} lang="${lang}"`
}
textEdit:
existingCompletion.textEdit && TextEdit.is(existingCompletion.textEdit)
? {
range: existingCompletion.textEdit.range,
newText: `${existingCompletion.textEdit.newText} lang="${lang}"`
}
: undefined
})
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function executeExtractComponentCommand(

return <WorkspaceEdit>{
documentChanges: [
TextDocumentEdit.create(VersionedTextDocumentIdentifier.create(svelteDoc.uri, null), [
TextDocumentEdit.create(VersionedTextDocumentIdentifier.create(svelteDoc.uri, 0), [
TextEdit.replace(range, `<${componentName}></${componentName}>`),
createComponentImportTextEdit()
]),
Expand Down Expand Up @@ -91,7 +91,7 @@ async function executeExtractComponentCommand(
.map((tag) => tag.text)
.join('');

return TextDocumentEdit.create(VersionedTextDocumentIdentifier.create(newFileUri, null), [
return TextDocumentEdit.create(VersionedTextDocumentIdentifier.create(newFileUri, 0), [
TextEdit.insert(Position.create(0, 0), newText)
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
changes.map(async (change) => {
// Organize Imports will only affect the current file, so no need to check the file path
return TextDocumentEdit.create(
VersionedTextDocumentIdentifier.create(document.url, null),
VersionedTextDocumentIdentifier.create(document.url, 0),
change.textChanges.map((edit) => {
let range = mapRangeToOriginal(fragment, convertRange(fragment, edit.span));
// Handle svelte2tsx wrong import mapping:
Expand Down Expand Up @@ -131,10 +131,7 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
docs.set(change.fileName, doc);
}
return TextDocumentEdit.create(
VersionedTextDocumentIdentifier.create(
pathToUrl(change.fileName),
null
),
VersionedTextDocumentIdentifier.create(pathToUrl(change.fileName), 0),
change.textChanges.map((edit) => {
if (
fix.fixName === 'import' &&
Expand Down Expand Up @@ -214,10 +211,10 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
...refactor,
title: refactor.title
.replace(
'Extract to inner function in function \'render\'',
"Extract to inner function in function 'render'",
'Extract to function'
)
.replace('Extract to constant in function \'render\'', 'Extract to constant')
.replace("Extract to constant in function 'render'", 'Extract to constant')
}))
);
}
Expand Down Expand Up @@ -295,7 +292,7 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {

const documentChanges = edits?.edits.map((edit) =>
TextDocumentEdit.create(
VersionedTextDocumentIdentifier.create(document.uri, null),
VersionedTextDocumentIdentifier.create(document.uri, 0),
edit.textChanges.map((edit) => {
let range = mapRangeToOriginal(fragment, convertRange(fragment, edit.span));
// Some refactorings place the new code at the end of svelte2tsx' render function,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class UpdateImportsProviderImpl implements UpdateImportsProvider {
}

return TextDocumentEdit.create(
VersionedTextDocumentIdentifier.create(fragment.getURL(), null),
VersionedTextDocumentIdentifier.create(fragment.getURL(), 0),
change.textChanges.map((edit) => {
const range = mapRangeToOriginal(
fragment!,
Expand Down
9 changes: 3 additions & 6 deletions packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import {
ApplyWorkspaceEditParams,
ApplyWorkspaceEditRequest,
CodeActionKind,
createConnection,
DocumentUri,
IConnection,
IPCMessageReader,
IPCMessageWriter,
_Connection,
MessageType,
RenameFile,
RequestType,
Expand All @@ -17,6 +14,7 @@ import {
TextDocumentSyncKind,
WorkspaceEdit
} from 'vscode-languageserver';
import { IPCMessageReader, IPCMessageWriter, createConnection } from 'vscode-languageserver/node';
import { DiagnosticsManager } from './lib/DiagnosticsManager';
import { Document, DocumentManager } from './lib/documents';
import { Logger } from './logger';
Expand All @@ -36,7 +34,6 @@ namespace TagCloseRequest {
export const type: RequestType<
TextDocumentPositionParams,
string | null,
any,
any
> = new RequestType('html/tag');
}
Expand All @@ -46,7 +43,7 @@ export interface LSOptions {
* If you have a connection already that the ls should use, pass it in.
* Else the connection will be created from `process`.
*/
connection?: IConnection;
connection?: _Connection;
/**
* If you want only errors getting logged.
* Defaults to false.
Expand Down
4 changes: 1 addition & 3 deletions packages/language-server/test/plugins/css/CSSPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ describe('CSS Plugin', () => {
documentation: {
kind: 'markdown',
value:
'Defines character set of the document.\n\n[MDN Reference](https://developer.mozilla.org/docs/Web/CSS/@charset)'
'Defines character set of the document\\.\n\n[MDN Reference](https://developer.mozilla.org/docs/Web/CSS/@charset)'
},
sortText: 'd_0000',
textEdit: TextEdit.insert(Position.create(0, 7), '@charset'),
tags: []
});
Expand Down Expand Up @@ -338,7 +337,6 @@ describe('CSS Plugin', () => {
line: 0
}
}

});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import sinon from 'sinon';
import { Position } from 'vscode-languageserver';
import { Document } from '../../../src/lib/documents';
import * as importPackage from '../../../src/importPackage';
import { SvelteDocument, TranspiledSvelteDocument } from '../../../src/plugins/svelte/SvelteDocument';
import {
SvelteDocument,
TranspiledSvelteDocument
} from '../../../src/plugins/svelte/SvelteDocument';
import * as configLoader from '../../../src/lib/documents/configLoader';

describe('Svelte Document', () => {
Expand Down Expand Up @@ -56,9 +59,11 @@ describe('Svelte Document', () => {
return Promise.resolve({
code: getSourceCode(true),
dependencies: [],
toString: () => getSourceCode(true)
toString: () => getSourceCode(true),
map: <any>null
});
},
walk: <any>null,
VERSION: <any>'',
compile: <any>null,
parse: <any>null
Expand Down
Loading